Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quite some time ago I read a post here where someone needed a similar ability, and I made a mental note to remember it. *However, I am not putting the correct criteria in my search so I can't refind it. *

    My challenge: *

    I have a string that contains the drive letter, the path and the file name in the following format:

    C:\MainPath\SubFolder\MyFile.txt
    I need to capture just the "MyFile.txt" and cannot figure out how.

    Thanks in advance for your assitance.

    Ken







  2. #2
    WS Lounge VIP
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    8,177
    Thanks
    47
    Thanked 983 Times in 913 Posts
    I would do a backward search (right to left) for "\" and collect everything right of that.

    cheers, Paul

  3. #3
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts
    This worked.

    Dim i As Integer


    For i = 1 To Len(FileName)
    ****If Left(Right(FileName, i), 1) = "\" Then
    ********strFilenameonly = Right(FileName, i - 1)


    * * * * i = Len(FileName)
    ****End If
    Next i





    Thanks

  4. #4
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You can do it a lot quicker using InstrRev

    This looks for the first occurence from the right of a given character.
    Since a filename cannot contain a \ character in it, then the filename must start 1 character after the last \

    See example below

    Code:
    Dim strFile as string, strFileName as string
    
    strFile = "C:\MainPath\SubFolder\MyFile.txt"
    strFilename = Mid(strFile, InStrRev(strFile, "\") + 1)
    
    msgbox strFilename
    Andrew

  5. #5
    4 Star Lounger
    Join Date
    Jan 2003
    Location
    Central Florida, USA
    Posts
    505
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Awesome. *Just another function (InStrRev) that I was not aware of.




    THANKS! ! !*


Posting Permissions

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