CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 1999
    Location
    Michigan, USA
    Posts
    29

    Breaking up a string

    I have a string that contains a full path to a file. I want to break it up so that I have 3 strings that have the drive, the path, and the file. The drive string was easy to get with Left(), but now I run into the problem of the path and file name. The path is variable.


  2. #2
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: Breaking up a string

    Use InStr to find the first occurrence of \
    Use InstrRev to find the last occurrence of \
    Knowing these two positions, you can
    Use Left$ to extract the drive
    Use Mid$ to extract the portion between these two positions, it will be your path
    Use Right$ to extract the filename


  3. #3
    Join Date
    Jan 2001
    Posts
    165

    Re: Breaking up a string

    dim sFile as string, sDrive as string, sPath as string, sFileName as string

    sFile = "C:\Program Files\Project 1\Project1.exe"

    sDrive = Left(sFile, 1)
    sPath = Mid(sFile, Instr(sFile, "\"), InstrRev(sFile, "\") - Instr(sFile, "\") + 1)
    sFileName = Right(sFile, Len(sFileName) - InstrRev(sFile, "\"))

    A bit crude but I like shortcuts

    -K


Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured