Results 1 to 2 of 2

Thread: VB Snippet - File Name without Extension

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    VB Snippet - File Name without Extension

    VB Code:
    1. Public Function FileNameWOExt(FileName As String) As String
    2.  
    3.      Dim i As Integer
    4.  
    5.      Dim lpFileName As String
    6.      
    7.      On Error Resume Next
    8.      
    9.      For i = Len(FileName) To 1 Step -1
    10.  
    11.        If Mid(FileName, i, 1) = "\" Then
    12.  
    13.          Exit For
    14.  
    15.        End If
    16.  
    17.      Next
    18.      
    19.      lpFileName = Mid$(FileName, i + 1)
    20.  
    21.      i = InStr(Mid(FileName, i + 1), ".")
    22.  
    23.      If i > 1 Then
    24.  
    25.       GetFileName = Mid(lpFileName, 1, i - 1)
    26.  
    27.      End If
    28.  
    29. End Function
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    Fanatic Member
    Join Date
    Jun 2000
    Location
    Forest
    Posts
    545
    James,

    I think you need to change GetFileName to FileNameWOExt because your function is not working properly.

    Also, I like to point out something and hope you don't mind. WindowXP and most of the OS now a day allows period in a file name. For example, My.Admin.asp. Using your function, against a file like that, will only return "My".

    Here are two alternatives.
    Code:
    Private Sub Form_Load()
        'Alternative 1
        MsgBox FileNameWithoutExt("Z:\Update\Done\My.Admin.asp")
        
        'Alternative 2
        'Dim fso As New FileSystemObject
        Dim fso As Object
        Set fso = CreateObject("Scripting.FileSystemObject")
        MsgBox fso.GetBaseName("Z:\Update\Done\My.Admin.asp")
    End Sub
    
    Function FileNameWithoutExt(strFileName As String) As String
        Dim intBackSlash As Integer
        Dim intPeriod As Integer
        intBackSlash = InStrRev(strFileName, "\", Len(strFileName), vbTextCompare) + 1
        intPeriod = InStrRev(strFileName, ".", Len(strFileName), vbTextCompare)
        
        If intPeriod = 0 Then intPeriod = Len(strFileName) + 1  'Incase there is no extension
        FileNameWithoutExt = Mid(strFileName, intBackSlash, intPeriod - intBackSlash)
    End Function
    Bird of Prey

    Mr. Bald Eagle.
    [img][/img]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width