Retrieving short path name
By: Lorenzo Dalla Vecchia Date: 16/04/2000

This tip shows you how to retrieve the short path name for a file. A name like "C:\My Documents\Long Name.doc" becomes "C:\MyDocu~1\LongNa~1.doc".
It is possible through the GetShortPathName API function.

The following is the API declaration to paste into the Declarations section of a module:

Declare Function GetShortPathName Lib "kernel32" Alias _
"GetShortPathNameA" (ByVal lpszLongPath As String, ByVal _
lpszShortPath As String, ByVal lBuffer As Long) As Long

Copy the following function into a form/module:

Public Function GetShortPath(strFileName As String) _
As String Dim lngRes As Long
    Dim strPath As String
    strPath = String$(165, 0)
    lngRes = GetShortPathName(strFileName, strPath, 164)
    GetShortPath = Left$(strPath, lngRes)
End Function

The use is very easy. Here's an example:

Dim ShortPath As String
ShortPath = GetShortPath("C:\My Documents\Long Name.doc"

ShortPath will be set to "C:\MyDocu~1\LongNa~1.doc".

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it