Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleGet long file names and paths for command line arguments
Keywordscommand line argument, file name, long file name, long path name
CategoriesFiles 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 _
    String
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
    Else
        result = ""
        pos = 1
    End If

    ' 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)
        Else
            long_name = Dir$(Left$(short_name, pos - 1), _
                vbNormal + vbHidden + vbSystem + _
                vbDirectory)
        End If
        result = result & "\" & long_name
    Loop

    LongFileName = result
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated