Results 1 to 14 of 14
  1. #1
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Extract Filename (vb6)

    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 ?

  2. #2
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Extract Filename (vb6)

    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

  3. #3
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    Mark, thanks for that.
    Does this look OK:

    <pre>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</pre>


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

  4. #4
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Extract Filename (vb6)

    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

  5. #5
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    Thanks Mark.

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

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    Hi Dave,

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

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


    Six of one, half dozen of the other ...

  7. #7
    Silver Lounger
    Join Date
    Jun 2002
    Location
    Cheadle, Staffordshire
    Posts
    2,177
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    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 <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

  8. #8
    Bronze Lounger
    Join Date
    Nov 2001
    Location
    Arlington, Virginia, USA
    Posts
    1,394
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Extract Filename (vb6)

    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.

  9. #9
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    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. <img src=/S/sorry.gif border=0 alt=sorry width=15 height=15>

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

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


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

  10. #10
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract Filename (vb6)

    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

  11. #11
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    Thanks, Kevin

    FYI:
    From "VB & VBA in a Nutshell"
    <hr>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.
    <hr>

  12. #12
    New Lounger
    Join Date
    Jul 2002
    Location
    Auckland, New Zealand
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    MsgBox Dir("C:ExcelFilesHelpactionscript_dict.pdf")

    Will yield the File name if it exists

  13. #13
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extract Filename (vb6)

    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.
    Charlotte

  14. #14
    New Lounger
    Join Date
    Jul 2002
    Location
    Auckland, New Zealand
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extract Filename (vb6)

    Yes, and I stated that when posting.

    Original Q was
    " need to extract a filename from a string....."

Posting Permissions

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