
Get long file name from short file name
Just for fun, here's three functions, the first one returns the short
filename, the other two return the long filename.
'**************** Code Start *********************************
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA"
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal
cchBuffer As Long) As Long
'Returns the Path and File in short (8.3) format
'If the file doesn't exist returns ""
Function ShortName(lpszLongPath As String) As String
Dim lpszShortPath As String
Dim cchBuffer As Long
Dim lngret As Long
lpszLongPath = lpszLongPath
lpszShortPath = String$(255, 0)
cchBuffer = Len(lpszShortPath)
lngret = GetShortPathName(lpszLongPath, lpszShortPath, cchBuffer)
If lngret > cchBuffer Then
lpszShortPath = String$(lngret, 0)
cchBuffer = Len(lpszShortPath)
lngret = GetShortPathName(lpszLongPath, lpszShortPath, cchBuffer)
End If
ShortName = Left(lpszShortPath, lngret)
End Function
'**************** Code End ***********************************
'**************** Code Start *********************************
Private Const MAX_PATH& = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" _
(ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
'Returns the Path and File in Long format
'If the file doesn't exist returns ""
Function LongName(ByVal lpFileName As String) As String
Dim lpFindFileData As WIN32_FIND_DATA
Dim strRet As String
Dim strTemp As String
Dim strtemp2 As String
Dim lngret As Long
strtemp2 = lpFileName
Do Until lngret = -1
lngret = FindFirstFile(lpFileName, lpFindFileData)
If lngret <> -1 Then
strTemp = lpFindFileData.cFileName
strTemp = Left(strTemp, InStr(strTemp, vbNullChar) - 1)
Do Until Right(lpFileName, 1) = "\"
lpFileName = Left(lpFileName, Len(lpFileName) - 1)
Loop
lpFileName = Left(lpFileName, Len(lpFileName) - 1)
If strRet = "" Then
strRet = strTemp
Else
strRet = strTemp & "\" & strRet
End If
End If
Loop
If strtemp2 <> lpFileName Then strRet = lpFileName & "\" & strRet
LongName = strRet
End Function
'**************** Code End ********************************
Or this version uses the Dir function to get the long filename
'**************** Code Start ********************************
'Returns the Path and File in Long format
'If the file doesn't exist returns ""
Function DirLongName(ByVal lpFileName As String) As String
Dim strRet As String
Dim strTemp As String
Dim lngret As Long
strTemp = Dir(lpFileName)
strRet = strTemp
If strRet <> "" Then
Do Until strTemp = "."
Do Until Right(lpFileName, 1) = "\"
lpFileName = Left(lpFileName, Len(lpFileName) - 1)
Loop
lpFileName = Left(lpFileName, Len(lpFileName) - 1)
strTemp = Dir(lpFileName, vbDirectory)
If strTemp <> "." Then strRet = strTemp & "\" & strRet
Loop
strRet = lpFileName & "\" & strRet
End If
DirLongName = strRet
End Function
'**************** Code End ********************************
Quote:
>Hi people,
>Does anyone knows how I can get the long name of a file (win95) from its
>short name?
>Ex: C:\progra~1\budget.txt is in fact C:\Program Files\budget.txt
>I need a way in Access97 (visual basic coding) to convert these shorts
>names to long ones.... I've tried CURDIR(), DIR(), etc. but it doesn't
>work!
>Any idea? I know I'm out!
>Jean