Don, here's a routine that does it through the API which is probably
preferred, as well as an improved Error Handler in the Non-API version I
sent in the last message, in case you don't want to use API calls.
*** API Version Start of Code
Option Explicit
Private Declare Function GetLongPathName Lib "kernel32" Alias
"GetLongPathNameA" (ByVal lpShortPath As String, ByVal lpLongPath As String,
ByVal nBufferLength As Long) As Long
Private Const MAX_PATH = 260
Private Function GetLongFilePath(Path As String) As String
On Error GoTo ErrorHandler
' Pass a short folder/file string to Path, and this will return the long
version.
' i.e. C:\MyDocu~1\ becomes C:\My Documents
' If you include a trailing backslash, it will be remain, if not, it will
not be added.
Dim strLongPath As String ' Stores the Long folder/file path
Dim strShortPath As String '
Dim lngBufferLength As Long
strLongPath = Space(MAX_PATH) ' Prepare the Long path buffer
lngBufferLength = GetLongPathName(Path & Chr(0), strLongPath,
Len(strLongPath)) ' Try to get the long path
If lngBufferLength > 0 Then ' If so, then we have a path! if not, the
path/file does not exist
If lngBufferLength > Len(strLongPath) Then ' Wasn't a big enough buffer!
must resize and recall
strLongPath = Space(lngBufferLength) ' Resize the buffer to the required
size
lngBufferLength = GetLongPathName(Path & Chr(0), strLongPath,
Len(strLongPath)) ' Recall the API
End If
strLongPath = Left(strLongPath, lngBufferLength) ' Trim any extra
characters off the long path buffer
Else ' Path/File does not exist
Err.Raise 53 ' Raise 'File not found" error
End If
GetLongFilePath = strLongPath ' Return the long path
Exit Function ' Leave before the error handler
ErrorHandler:
If Err.Number = 53 Then ' File not found
MsgBox Err.Description & vbCrLf & "'" & Path & "'", vbExclamation +
vbOKOnly, "GetLongFilePath Error"
Else ' Unhandled error...uh oh
MsgBox "Unhandled error in GetLongFilePath: " & vbCrLf & Err.Description,
vbCritical + vbOKOnly, "GetLongFilePath Error"
End If
End Function
*** API Version END of code
*** Non-API Version Start of code
Public Function GetLongFilePath(Path As String) As String
On Error GoTo ErrorHandler
' - Path string must be a fully qualified path (i.e.
C:\Mydocu~1\testlo~1.txt")
' including DRIVE:\FOLDERS (If Any)\File (If any)
' - Does not support wildcards
' - Path strings that do not contain a trailing backslash will
' be treated as file paths, those that do contain a trailing
' backslash will be treated as folder paths.
Dim avarPaths As Variant ' Stores each segment of the path
Dim lngPathCounter As Long ' Counter for the path array
Dim strLongPath As String ' The long file/folder path to be returned
Dim strPath As String ' Holds the results of the DIR function for the
current path in the array
Dim lngLBound As Long ' Holds the LBound of the path array
Dim lngUBound As Long ' Holds the UBound of the path array
avarPaths = Split(Path, "\") ' Split the path string into its constituent
drive/paths/file
lngLBound = LBound(avarPaths) ' Store the lowest element of the array
lngUBound = UBound(avarPaths) ' Store the highest element of the array
strLongPath = avarPaths(lngLBound) & "\" ' The drive letter should be the
first element, so store it as the first part of the long path
For lngPathCounter = lngLBound + 1 To lngUBound - 1 ' Count from the first
element after the drive letter, to the last element before the filename (or
the empty string if we are expanding a folder path)
strPath = Dir(strLongPath & avarPaths(lngPathCounter), vbDirectory) ' See
if we can find this folder
If strPath <> "" Then ' We found either a folder or a file
strLongPath = strLongPath & strPath ' Add the path we just found to the
full long path
If Not GetAttr(strLongPath) And vbDirectory Then ' Check to see if the
path we found was indeed a directory, if this test succeeds, we found a FILE
and not a directory
Err.Raise 76 ' Raise a "Path Not Found" error, because we're looking for
a folder and not a file at this point.
End If
strLongPath = strLongPath & "\" ' Append a trailing backslash to the long
path.
Else ' No folder/file was found at this path,
Err.Raise 76 ' So raise a "Path Not Found" error.
End If
Next lngPathCounter ' Loop through the folders in the array
If avarPaths(lngUBound) <> "" Then ' If this not blank, we must search for
the file, otherwise, our work is done!
strPath = Dir(strLongPath & avarPaths(lngUBound), vbArchive Or vbHidden Or
vbReadOnly Or vbSystem) ' Test for the existance of the file
If strPath <> "" Then ' File exists
strLongPath = strLongPath & strPath ' Append the long file name to the
long path
Else ' File doesn't exist
Err.Raise 53 ' Raise a "File not found" error
End If
End If
GetLongFilePath = strLongPath ' Pass the long path to the function
Exit Function ' Leave before the error handler
ErrorHandler:
Select Case Err.Number
Case 53, 76 ' File not found, Path not found
MsgBox Err.Description & vbCrLf & "'" & Path & "'", vbExclamation +
vbOKOnly, "GetLongFilePath Error"
Case 9
MsgBox "No path to expand.", vbExclamation + vbOKOnly, "GetLongFilePath
Error"
Case Else ' Unhandled error...uh oh
MsgBox "Unhandled error in GetLongFilePath: " & vbCrLf & Err.Number & " -
" & Err.Description, vbCritical + vbOKOnly, "GetLongFilePath Error"
End Select
End Function
*** NON-API Version End of code
Quote:
> How can I convert a shortened long file name back to its original long
name?
> For example, if the original long name is
> my document.doc
> but the name I'm being sent is
> mydocu~1.doc
> How can I get the original name back? Assuming, of course that the
> file still exists.
> How can I do the same for folder names?
> --
> Don Sayler
> for email reponses, remove
> 'do.not.spam.me.' from my return address