• Parsing string (from the right)

    Author
    Topic
    #465624

    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:

    [indent]C:MainPathSubFolderMyFile.txt

    [/indent]I need to capture just the “MyFile.txt” and cannot figure out how.

    Thanks in advance for your assitance.

    Ken

    Viewing 3 reply threads
    Author
    Replies
    • #1197294

      I would do a backward search (right to left) for “” and collect everything right of that.

      cheers, Paul

    • #1197295

      This worked.

      Dim i As Integer

      For i = 1 To Len(FileName)
      [indent]    If Left(Right(FileName, i), 1) = “” Then
      [/indent][indent][indent]        strFilenameonly = Right(FileName, i – 1)

              i = Len(FileName)

      [/indent][/indent][indent]    End If
      [/indent]Next i

      Thanks

    • #1197298

      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:MainPathSubFolderMyFile.txt"
      strFilename = Mid(strFile, InStrRev(strFile, "") + 1)
      
      msgbox strFilename
      
    • #1197357

      Awesome.  Just another function (InStrRev) that I was not aware of.

      THANKS! ! ! 

    Viewing 3 reply threads
    Reply To: Parsing string (from the right)

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

    Your information: