delete part of a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbanning
    New Member
    • Apr 2007
    • 19

    delete part of a string

    I am trying to remove part of a string and place it into another variable

    the string is a filename

    eg

    "C:\help\demo\s ample.txt"

    I want to remove and resave into a new string just the file name i.e "sample.txt "

    my thinking was to count backward to the point where the last "\" was and store this integer value and then delete all parts before.

    another idea was to split the string into 2 parts and store the "sample.txt " in one and the rest in another.

    I am a bit stuck at present with this and was hoping to be pointed in the right direction?

    i.e the file name will change continuously so the best method would be just the lacte the last "\" and either spilt or delete before.

    Can you help?

    Thanks
  • Robbie
    New Member
    • Mar 2007
    • 180

    #2
    I'd do exactly what you say.
    [code=vb]
    Dim TempPos As Integer
    'Find position of the last slash
    TempPos = InStrRev(PathSt ring, "\")
    'Get all text after this position
    FileString = Mid(PathString, TempPos + 1)
    [/code]
    PathString is the string containing the full path + filename.
    FileString is what will store just the filename part.
    Last edited by Killer42; Aug 20 '07, 02:55 AM. Reason: Added =vb to CODE tag, corrected slash to backslash

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Thanks for that, Robbie. It's interesting to note that this should work even if there's no path in the original string.

      Comment

      Working...