Short DOS Path

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

immediate window showing short path function

Don’t forget to set a reference to Microsoft Scripting Library (Tools – References)

Posted in Uncategorized

5 thoughts on “Short DOS Path

  1. Something like this doesn’t have the scripting library overhead:

    Private Declare Function GetShortDOSPathName Lib “Kernel32” _
                                                 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

  2. 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.

  3. 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.

Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.