Remember DOS? Back in the day, we had to limit our file names to eight characters plus a three digit extension (8.3) and we liked it. Today I needed to add Google Chrome to my startup batch file. Normally I can convert long Windows paths to short DOS paths in my head, but I couldn’t remember what do with spaces (just eliminate them), so I wrote this to figure out the short path.
Function GetShortDosPath(sPath As String)
Dim fso As Scripting.FileSystemObject
Dim fsoFile As Scripting.file
Set fso = New FileSystemObject
Set fsoFile = fso.GetFile(sPath)
GetShortDosPath = fsoFile.ShortPath
End Function
Dim fso As Scripting.FileSystemObject
Dim fsoFile As Scripting.file
Set fso = New FileSystemObject
Set fsoFile = fso.GetFile(sPath)
GetShortDosPath = fsoFile.ShortPath
End Function
Don’t forget to set a reference to Microsoft Scripting Library (Tools – References)
Something like this doesn’t have the scripting library overhead:
Alias “GetShortPathNameA” _
(ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) _
As Long
Function GetShortName(ByVal LongPath As String) As String
Dim strShortPath As String
Dim lngBuffer As Long
Dim lngRet As Long
strShortPath = String$(1024, 0)
lngBuffer = Len(strShortPath)
lngRet = GetShortDOSPathName(LongPath, strShortPath, lngBuffer)
GetShortName = Left(strShortPath, lngRet)
End Function
Or, since this is for a batch file anyway, in the batch file
for %%f in () do set SHORTFILENAME=%%~sf
There ARE times VBA is NOT the best tool for the task.
@#$% angle brackets!
for %%f in ([long file name]) do set SHORTFILENAME=%%~sf
Still using two syntax highlighter plugins, eh?
Your function doesn’t declare a return value type. Also, you can use CreateObject and avoid the early binding.
Dick, pls check your gmail.
Thanks,
Stuart