|Title||Get long file names and paths for command line arguments|
|Keywords||command line argument, file name, long file name, long path name|
|Categories||Files and Directories, Software Engineering|
If you drag a file onto an executable program written in VB, or use Explorer's SentTo command to send files to it, that program can use the Command$ statement to get a space-separated list of the files that were dragged. Because the list uses spaces to separate the files, the file names cannot contain spaces.
To avoid this issue, the file names are the short, DOS file names. This example shows how to convert those names into nice long versions.
One problem with the example Get long file names for command line arguments is that it expands only the file names not their paths.
This example uses the LongFileName function to expand a file's name and path. This function breaks the short name into directory pieces and uses Dir$ to get the long name for each piece.
To test the program, build its executable. Then drag files with long file names or names that contain spaces (this Zip file contains one) onto the exe file.
' Return the file's long name.
Public Function LongFileName(ByVal short_name As String) As _
Dim pos As Integer
Dim result As String
Dim long_name As String
' Start after the drive letter if any.
If Mid$(short_name, 2, 1) = ":" Then
result = Left$(short_name, 2)
pos = 3
result = ""
pos = 1
' Consider each section in the file name.
Do While pos > 0
' Find the next \.
pos = InStr(pos + 1, short_name, "\")
' Get the next piece of the path.
If pos = 0 Then
long_name = Dir$(short_name, vbNormal + _
vbHidden + vbSystem + vbDirectory)
long_name = Dir$(Left$(short_name, pos - 1), _
vbNormal + vbHidden + vbSystem + _
result = result & "\" & long_name
LongFileName = result