• Extract Filename (vb6)

    Author
    Topic
    #404859

    I need to extract a filename from a string.

    If my users were to name files exactly as they should be, then I could possibly extract from the length of the string.
    i.e. Right$(Filename),13 but users don’t always conform to what they should do.

    So the string would be:

    L:MMpdfEstPDF12345-est.pdf
    L:MMpdfEstPDF12345-int.pdf
    L:MMpdfEstPDF12345-aut.pdf

    But they on occasion name

    L:MMpdfEstPDF12345-est.pdf
    L:MMpdfEstPDF12345-int.pdf
    L:MMpdfEstPDF12345-auth.pdf
    L:MMpdfEstPDF12345-part.pdf

    In this occasion the characters would be 14 because of the auth & the part after the number.
    Is there a function to extract to the last separator making the filename always extractable in full ?

    Viewing 5 reply threads
    Author
    Replies
    • #826783

      I usually use expression or function like this example:

      Public Function GetFileNameFromPath(ByRef strPath As String) As String

      If Len(strPath) = 0 Or (InStr(1, strPath, “”, vbBinaryCompare) = 0) Then
      GetFileNameFromPath = strPath
      Else
      GetFileNameFromPath = Mid$(strPath, InStrRev(strPath, “”, , vbBinaryCompare) + 1)
      End If

      End Function

      Examples:

      ? GetFileNameFromPath(“C:Program FilesMicrosoft OfficeOfficeMSAccess.exe”)
      MSAccess.exe

      ? GetFileNameFromPath(“MSAccess.exe”)
      MSAccess.exe

      ? GetFileNameFromPath(“C:BatchFile.txt”)
      BatchFile.txt

      Using Mid & InStrRev functions are easiest way to do this, just find the last “” and add 1 to determine where to start; with Mid, unlike Right & Left functions, you don’t have to specify how many characters to return; if last argument left blank, will return rest of the original text string from start point. If you are sure a valid path is always going to be provided, you don’t have to use function, just use the Mid$(strPath, InStrRev(strPath, “”, , vbBinaryCompare) + 1) expression, where strPath is variable representing path.

      HTH

      • #826793

        Mark, thanks for that.
        Does this look OK:

        Public Function GetFileNameFromPath(ByRef strPath As String) As String
        
            If Len(strPath) = 0 Or (InStr(1, strPath, "", vbBinaryCompare) = 0) Then
                GetFileNameFromPath = strPath
            Else
                GetFileNameFromPath = Mid$(strPath, InStrRev(strPath, "", , vbBinaryCompare) + 1)
            End If
        
        End Function
        
        Private Sub procDelDoc_Click(Index As Integer)
             If AcrobatPath.Text = "L:MMPDFUtilitiesPDFViewer.pdf" Then Exit Sub
             
             If MsgBox("You Are About To Archive This Document !!." & _
                       " Do You Want To Continue ? ", vbYesNo + vbQuestion, " Archive Document") = vbYes Then
                  WriteLog GetComputerName & " " & GetNetUser & _
                            " *** DOCUMENT ARCHIVED ***" & " " & Me.AcrobatPath
        
        
                  Dim AF As String     'Archive Folder
                  Dim FN As String     'File
                  Dim strFolderName As String
                  Dim File As String
        
        
                  AF = "L:MMpdfArchive"
                  File = Me.AcrobatPath
                   
                  strFolderName = AF & Me.txtEst.Text & ""
        
                  If Dir(strFolderName, vbDirectory) = "" Then
                       MkDir strFolderName
                  End If
        
                  FileCopy File, strFolderName & GetFileNameFromPath(File)
                  Kill AcrobatPath
                  Call cmdList_Click
             End If
        
        End Sub

        Or would it be better to create a standard module for the GetfileName routine ?

        • #826798

          I assume this code is running in some kind of form module? If so, and this is the only place in program where you need to use the GetFileNameFromPath function, then may as well leave it in the form module, and should be declared as Private (tho’ OK to leave as is). If function may be needed elsewhere in program, then should move to standard code module and ensure declared as Public so can be called from anywhere in program.

          HTH

          • #826808

            Thanks Mark.

            It is used in two instances, and could be useful elsewhere at a later date.
            I shall take the latter advice.

          • #826809

            Thanks Mark.

            It is used in two instances, and could be useful elsewhere at a later date.
            I shall take the latter advice.

        • #826799

          I assume this code is running in some kind of form module? If so, and this is the only place in program where you need to use the GetFileNameFromPath function, then may as well leave it in the form module, and should be declared as Private (tho’ OK to leave as is). If function may be needed elsewhere in program, then should move to standard code module and ensure declared as Public so can be called from anywhere in program.

          HTH

      • #826794

        Mark, thanks for that.
        Does this look OK:

        Public Function GetFileNameFromPath(ByRef strPath As String) As String
        
            If Len(strPath) = 0 Or (InStr(1, strPath, "", vbBinaryCompare) = 0) Then
                GetFileNameFromPath = strPath
            Else
                GetFileNameFromPath = Mid$(strPath, InStrRev(strPath, "", , vbBinaryCompare) + 1)
            End If
        
        End Function
        
        Private Sub procDelDoc_Click(Index As Integer)
             If AcrobatPath.Text = "L:MMPDFUtilitiesPDFViewer.pdf" Then Exit Sub
             
             If MsgBox("You Are About To Archive This Document !!." & _
                       " Do You Want To Continue ? ", vbYesNo + vbQuestion, " Archive Document") = vbYes Then
                  WriteLog GetComputerName & " " & GetNetUser & _
                            " *** DOCUMENT ARCHIVED ***" & " " & Me.AcrobatPath
        
        
                  Dim AF As String     'Archive Folder
                  Dim FN As String     'File
                  Dim strFolderName As String
                  Dim File As String
        
        
                  AF = "L:MMpdfArchive"
                  File = Me.AcrobatPath
                   
                  strFolderName = AF & Me.txtEst.Text & ""
        
                  If Dir(strFolderName, vbDirectory) = "" Then
                       MkDir strFolderName
                  End If
        
                  FileCopy File, strFolderName & GetFileNameFromPath(File)
                  Kill AcrobatPath
                  Call cmdList_Click
             End If
        
        End Sub

        Or would it be better to create a standard module for the GetfileName routine ?

    • #826784

      I usually use expression or function like this example:

      Public Function GetFileNameFromPath(ByRef strPath As String) As String

      If Len(strPath) = 0 Or (InStr(1, strPath, “”, vbBinaryCompare) = 0) Then
      GetFileNameFromPath = strPath
      Else
      GetFileNameFromPath = Mid$(strPath, InStrRev(strPath, “”, , vbBinaryCompare) + 1)
      End If

      End Function

      Examples:

      ? GetFileNameFromPath(“C:Program FilesMicrosoft OfficeOfficeMSAccess.exe”)
      MSAccess.exe

      ? GetFileNameFromPath(“MSAccess.exe”)
      MSAccess.exe

      ? GetFileNameFromPath(“C:BatchFile.txt”)
      BatchFile.txt

      Using Mid & InStrRev functions are easiest way to do this, just find the last “” and add 1 to determine where to start; with Mid, unlike Right & Left functions, you don’t have to specify how many characters to return; if last argument left blank, will return rest of the original text string from start point. If you are sure a valid path is always going to be provided, you don’t have to use function, just use the Mid$(strPath, InStrRev(strPath, “”, , vbBinaryCompare) + 1) expression, where strPath is variable representing path.

      HTH

    • #826868

      Hi Dave,

      Here’s an alternate, platform-independent way to get the filename without the separator:

      Function GetFileName(sPath As String) As String
      GetFileName = Right$(sPath, (Len(sPath) - (InStrRev(sPath, Application.PathSeparator))))
      End Function

      Six of one, half dozen of the other …

      • #826890

        Thanks Andrew, I will put this in my collection of Utilities Modules.

        Marks function works ok in testing, so for now it will stick with it.

        Thanks again all thankyou

      • #826891

        Thanks Andrew, I will put this in my collection of Utilities Modules.

        Marks function works ok in testing, so for now it will stick with it.

        Thanks again all thankyou

      • #826948

        Doesn’t Application.PathSeparator only apply to Word & Excel (& PPT?), that have MacIntosh versions? (The Mac’s path separator is a colon). I think Dave’s application is strictly VB6 – AFAIK there’s no such thing as “Visual Basic for the Mac” (though at one time there was something called “Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0” that you still see referenced in archaic MSKB articles) – I always thought that VB only “did Windows”, with the exception of VBA in those Office apps that have Mac versions. If so, in this case I wouldn’t worry about using backslash () as path separator in function.

        • #826956

          Hi Mark,

          Yeah, that’s correct. The Application object is not part of VB, it comes from the hosted application. That particluar function came from some code I used in a Word template that was to be run on Windows and Mac. My bad. sorry

          Maybe a better choice would be to provide the path separator as an argument to the function:

          Function GetFileName(sPath As String, sSeparator As String) As String
          GetFileName = Right$(sPath, (Len(sPath) - (InStrRev(sPath, sSeparator))))
          End Function

          That would provide a more generally usable function that might come in handy when parsing a URL, etc.

        • #826957

          Hi Mark,

          Yeah, that’s correct. The Application object is not part of VB, it comes from the hosted application. That particluar function came from some code I used in a Word template that was to be run on Windows and Mac. My bad. sorry

          Maybe a better choice would be to provide the path separator as an argument to the function:

          Function GetFileName(sPath As String, sSeparator As String) As String
          GetFileName = Right$(sPath, (Len(sPath) - (InStrRev(sPath, sSeparator))))
          End Function

          That would provide a more generally usable function that might come in handy when parsing a URL, etc.

      • #826949

        Doesn’t Application.PathSeparator only apply to Word & Excel (& PPT?), that have MacIntosh versions? (The Mac’s path separator is a colon). I think Dave’s application is strictly VB6 – AFAIK there’s no such thing as “Visual Basic for the Mac” (though at one time there was something called “Microsoft QuickBASIC Compiler for the Apple Macintosh 1.0” that you still see referenced in archaic MSKB articles) – I always thought that VB only “did Windows”, with the exception of VBA in those Office apps that have Mac versions. If so, in this case I wouldn’t worry about using backslash () as path separator in function.

    • #826869

      Hi Dave,

      Here’s an alternate, platform-independent way to get the filename without the separator:

      Function GetFileName(sPath As String) As String
      GetFileName = Right$(sPath, (Len(sPath) - (InStrRev(sPath, Application.PathSeparator))))
      End Function

      Six of one, half dozen of the other …

    • #827115

      Attached ia a module containing a function GetSingleFileInfo that takes 2 parameters, the file name and a structure declared in the module that describes the file.

      It doesn’t use any delimiters to get the information but uses the Scripting.FileSystemObject and a couple of API calls instead.

      Regards,
      Kevin Bell

      • #827443

        Thanks, Kevin

        FYI:
        From “VB & VBA in a Nutshell”
        [indent]


        In stripping the “file extension” and returning the base name of Path, GetBaseName has no intelligence. That is, it doesn’t know whether the last component of Path is a path or a filename. If the last component includes one or more dots, it simply removes the last one, along with any following text. Hence, GetBaseName returns a null string for a Path of “.” and it returns “.” for a Path of “..”. It is, in other words, really a string manipulation function, rather than a file function.


        [/indent]

        • #830589

          MsgBox Dir(“C:ExcelFilesHelpactionscript_dict.pdf”)

          Will yield the File name if it exists

          • #832109

            However, it *only* works on files that already exist. If you need to get the filename only so you can locate it in another folder or move it, etc., that doesn’t help.

            • #832270

              Yes, and I stated that when posting.

              Original Q was
              ” need to extract a filename from a string…..”

            • #832271

              Yes, and I stated that when posting.

              Original Q was
              ” need to extract a filename from a string…..”

          • #832110

            However, it *only* works on files that already exist. If you need to get the filename only so you can locate it in another folder or move it, etc., that doesn’t help.

        • #830590

          MsgBox Dir(“C:ExcelFilesHelpactionscript_dict.pdf”)

          Will yield the File name if it exists

      • #827444

        Thanks, Kevin

        FYI:
        From “VB & VBA in a Nutshell”
        [indent]


        In stripping the “file extension” and returning the base name of Path, GetBaseName has no intelligence. That is, it doesn’t know whether the last component of Path is a path or a filename. If the last component includes one or more dots, it simply removes the last one, along with any following text. Hence, GetBaseName returns a null string for a Path of “.” and it returns “.” for a Path of “..”. It is, in other words, really a string manipulation function, rather than a file function.


        [/indent]

    • #827116

      Attached ia a module containing a function GetSingleFileInfo that takes 2 parameters, the file name and a structure declared in the module that describes the file.

      It doesn’t use any delimiters to get the information but uses the Scripting.FileSystemObject and a couple of API calls instead.

      Regards,
      Kevin Bell

    Viewing 5 reply threads
    Reply To: Extract Filename (vb6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: