Thanks to Patrice Goyer.
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.
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.
Use the FindFirstFile API function.
The main piece of code is the CompleteFileOpening subroutine. This routine takes a file name as input and changes it into a long file name. It calls subroutine ExtractFileNameAndPath to separate the file name and path. Then it uses the FindFirstFile API function to find the first file matching the file name and adds the long file name to the path.
|
Private Sub CompleteFileOpening(FileName As String)
Dim hFind As Long
Dim fileData As WIN32_FIND_DATA
Dim fn As String, fp As String
Dim s As String, sFile, lg As Long
On Error Resume Next
' First use FindFirstFile() to find the full name
' Pitfall: FindFirstFile() does not return the path name.
' It just returns the base name
' So we first split the file name to recall the file's
' location
ExtractFileNameAndPath FileName, fn, fp
hFind = FindFirstFile(FileName, fileData)
' Then we paste back the file name with the path
If fp = "" Then
FileName = stripNullChars(fileData.cFileName)
Else
FileName = fp & "\" & _
stripNullChars(fileData.cFileName)
End If
' free the "find" handle
Call FindClose(hFind)
' Finally we use the GetFullPathName() API function to
' get
' the long file name with its path
' Note: we make a first call with an empty string and
' length=0 in order to get the length of the full name
s = ""
lg = 0
sFile = ""
lg = GetFullPathName(FileName, lg, s, sFile)
s = Space(lg) ' get enough memory to contain the file
' name
sFile = Space(lg)
' now we make the actual call to get the value
lg = GetFullPathName(FileName, lg, s, sFile)
' return with the long path name
FileName = Left(s, lg) ' strip off the trailing '\0'
' of the C string
End Sub
' Split the filename to get basename and file location
' Note: this could also be done using common dialog control
' smartly
Private Sub ExtractFileNameAndPath(fullName As String, _
fName As String, fPath As String)
Dim i As Integer, l As Integer, c As String
Dim found As Boolean
l = Len(fullName)
If l = 0 Then fName = "": fPath = "": Exit Sub
found = False
For i = 1 To l
c = Mid(fullName, l - i + 1, 1)
If c = "\" Or c = ":" Then found = True: Exit For
Next i
If found Then
fName = Right(fullName, i - 1)
fPath = Left(fullName, l - i)
Else
fName = fullName
fPath = ""
End If
End Sub
|