How to Parse a Fully Qualified Path into Component Parts

ID: Q113897


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 1.0, 2.0, 3.0


SUMMARY

This article contains Visual Basic code you can use to break up a fully qualified path into its component parts.


MORE INFORMATION

The code in this article parses the fully qualified path into the following component parts:

  • Drive


  • Path


  • Filename


  • Extension


It parses the path from back to front (end to beginning) searching for the backslash (\) and colon (:) delimiters.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add a command button (Command1) and a text box (Text1) to Form1.


  3. Place the following code in the Command1_Click event:
    
       Sub Command1_Click ()
          TempPath$ = Text1
    
          DriveLetter = ""
          DirPath = ""
          Filename = ""
          Extension = ""
    
          If Mid(TempPath$, 2, 1) = ":" Then     ' Find the drive letter.
             DriveLetter = Left(TempPath$, 2)
             TempPath$ = Mid(TempPath$, 3)
          End If
    
          PathLength% = Len(TempPath$)
          For OffSet% = PathLength% To 1 Step -1  ' Find the next delimiter.
             Select Case Mid(TempPath$, OffSet%, 1)
    
                Case ".": ' This indicates either an extension or a . or a ..
                ThisLength% = Len(TempPath$) - OffSet%
                If ThisLength% >= 1 And ThisLength% <= 3 Then ' Extension
                   Extension = Mid$(TempPath$, OffSet%, ThisLength% + 1)
                End If
                TempPath$ = Left(TempPath$, OffSet% - 1)
    
                Case "\": ' This indicates a path delimiter.
                ThisLength% = Len(TempPath$) - OffSet%
                If ThisLength% >= 1 And ThisLength% <= 8 Then ' Filename
                   Filename = Mid$(TempPath$, OffSet% + 1, ThisLength%)
                   TempPath$ = Left(TempPath$, OffSet%)
                   FileNameFound% = True
                   Exit For
                End If
    
                Case Else
             End Select
          Next OffSet%
    
          If FileNameFound% = False Then
             Filename = TempPath$
          Else
             DirPath = TempPath$
          End If
    
          ' Print to form for display.
          Form1.Cls
          Form1.Print "Drive:", DriveLetter
          Form1.Print "Path:", DirPath
          Form1.Print "Filename:", Filename
          Form1.Print "Extension:", Extension
       End Sub 


  4. Press the F5 key to run the program.


  5. Type a path into the Text1 box.


  6. Click the Command1 button.


The program should break the path up into its component parts and print the result on Form1.

NOTE: This program does not attempt to validate the path structure. It assumes that the path you have typed in is a valid path.

Additional query words: 1.00 2.00 3.00 dissect parse break apart

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: September 17, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.