
Convert Long Folder Names to DOS Short Dir Names
Quote:
>Can someone please send me the source code for converting a Long path to a
>short dos path.
Private Declare Function SKGetLongPathName Lib "VB5STKIT.DLL" _
Alias "GetLongPathName" _
(ByVal LongPath$, ByVal ShortPath$, _
ByVal BufferSize&) As Long
Private Declare Function OSGetShortPathName& Lib "kernel32" _
Alias "GetShortPathNameA" _
(ByVal lpszLongPath$, ByVal lpszShortPath$, _
ByVal cchBuffer&)
Public Function ShortFileName(ByVal LongFileName$) As String
'-----------------------------------------------------------------------
' Purpose: Changes long filenames to it's equivalent short filename
' (DOS 8.3 format)
' Arguments: LongName (name of file)
' Returns: Short filename
' Notes: The file and directory must exist for this to work.
' 32-Bit function
'-----------------------------------------------------------------------
Const BufferSize = 300
Dim Buffer$
Dim Result&
On Error Resume Next
LongFileName$ = Trim$(LongFileName$)
If Len(LongFileName$) Then
Buffer$ = String(BufferSize, Chr$(0))
Result& = OSGetShortPathName&(LongFileName$, Buffer$, BufferSize)
If Result& <> 0 Then
ShortFileName = x_StripTerminator(Buffer$)
End If
End If
On Error GoTo 0
End Function
Public Function LongFileName(ByVal ShortFileName$) As String
' Purpose: Retrieve the long pathname version of a path possibly
' containing short subdirectory and/or file names
' Arguments: ShortFileName$ (name of file)
' Returns: Long filename
' Notes: The file and directory must exist for this to work.
' 32-Bit function
Const BufferSize = 300
Dim Buffer$
Dim Result&
On Error Resume Next
ShortFileName$ = Trim$(ShortFileName$)
If Len(ShortFileName$) Then
Buffer$ = String(BufferSize, Chr$(0))
Result& = SKGetLongPathName(ShortFileName$, Buffer$, BufferSize)
If Result& <> 0 Then
LongFileName = x_StripTerminator(Buffer$)
End If
End If
On Error GoTo 0
End Function
Private Function x_StripTerminator(ByVal Expression As String) As String
' Returns a string without any zero terminator. Typically,
' this was a string returned by a Windows API call.
'
' IN: [Expression] - String to remove terminator from
'
' Returns: The value of the string passed in minus any
' terminating zero.
Dim ZeroPos%
ZeroPos% = InStr(Expression, Chr$(0))
If ZeroPos% > 0 Then
x_StripTerminator = Left$(Expression, ZeroPos% - 1)
Else
x_StripTerminator = Expression
End If
End Function