How to Parse a Fully Qualified Path into Component Parts

Last reviewed: June 21, 1995
Article ID: Q113897
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic

  programming system for Windows, versions 1.0, 2.0, and 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 reference words: 1.00 2.00 3.00 dissect parse break apart
KBCategory: kbprg kbcode
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.