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
- Start a new project in Visual Basic. Form1 is created by default.
- Add a command button (Command1) and a text box (Text1) to Form1.
- 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
- Press the F5 key to run the program.
- Type a path into the Text1 box.
- 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.
|