Long File Names and Short File Names 
Author Message
 Long File Names and Short File Names

Does anyone know how to obtain the long file name from the short file name
and vice versa.



Fri, 07 Jul 2000 03:00:00 GMT  
 Long File Names and Short File Names

Use the GetShortPathName and GetFullPathName WinAPI functions.

------------
Jim Houghtaling

email:  hmm... no.
web:   working on it (heh)
platform(s):  Win95 os/r2, WinNT 4.0 sp3, VB5ee sp3
('till I get my page up go to VBNet...It's the best I've seen yet.)
http://home.sprynet.com/sprynet/rasanen/vbnet/default.htm
------------
It often shows an excellent command
of language to say nothing.



Quote:
> Does anyone know how to obtain the long file name from the short file
name
> and vice versa.



Fri, 07 Jul 2000 03:00:00 GMT  
 Long File Names and Short File Names

Thanks,

I tried the GetShortPathName and GetFullPathName functions right away, but
did not have any success with them. The always return a blank string to me.
The Daniel Appelman book even says that GetFullPathName is difficult to use
in VB, but he doesn't give an example of how to use it right. Do you have
any experience using these API functions successfully.

Joe



Sat, 08 Jul 2000 03:00:00 GMT  
 Long File Names and Short File Names

Man, am I stupid.

Ok, everybody.  (well, those of you who like to scroll through old
posts...)

I did not read the text of my referring document very well.  (my fault... I
was in a hurry).  To get the short path name of a file, you would use the
GetShortPathName API function (not hard at all) and I thought--when I
scanned the docs--that GetFullPathName was its complement.  Not so.

GetFullPathName will take a filename and, if there is no path in front of
the file name, prepend the current path and drive, and if there *is* a path
in front, it will return the string unchanged, but give you a pointer to
the filename in the returned string.  It does NO validation of file/path
names.  (and, like Appleman said, is more trouble than its worth)

However, what we need is the complement to GetShortPathName.  This,
according to the docs, is FindFirstFile.  Go figure.

I apologize for the length of this code sample, but I didn't see an easy
way around it.  Note that I did NOT do any playing with UNC names, so if
you're looking for them, you'll have to code for it.

    1.  Create a new project.
    2.  Place a CommandButton (Command1) on the default Form1.
    3.  Paste the code below into Form1's code window.
    4.  Run the program and click the button.

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

Private Declare Function FindFirstFile Lib "kernel32" Alias
"FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As
WIN32_FIND_DATA) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias
"GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As
String, ByVal cchBuffer As Long) As Long

Private Sub Command1_Click()

    Dim sShort As String
    Dim sLong As String

    sShort = GetShortName("c:\New Text Document.txt")
    sLong = GetLongName(sShort)

    Debug.Print "Short Name = " & sShort
    Debug.Print "Long Name = " & sLong

End Sub

Public Function GetShortName(LongName As String) As String

    Dim lpName As String
    Dim cbName As Long
    Dim rVal As Long

    cbName = MAX_PATH
    lpName = String$(cbName, 0)

    rVal = GetShortPathName(LongName, lpName, cbName)

    If rVal = 0 Then
        GetShortName = ""
    Else
        GetShortName = Left$(lpName, rVal)
    End If

End Function

Public Function GetLongName(ShortName As String) As String

    Dim fd As WIN32_FIND_DATA
    Dim strLongName As String, lpFileName As String, lpPathName As String
    Dim rVal As Long

    ' Get the filename
    rVal = FindFirstFile(ShortName, fd)
    lpFileName = Left$(fd.cFileName, InStr(fd.cFileName, Chr$(0)) - 1)

    ' Get the rest of the path
    lpPathName = Left$(ShortName, Len(ShortName) - (Len(fd.cAlternate) -
1))

    If Len(lpPathName) > 2 Then
        rVal = FindFirstFile(Left$(ShortName, Len(ShortName) -
(Len(fd.cAlternate) - 1)), fd)
        lpPathName = Left$(fd.cFileName, InStr(fd.cFileName, Chr$(0)) - 1)

        ' Concatenate the drive, path and file.
        GetLongName = Left$(ShortName, 2) & "\" & lpPathName & "\" &
lpFileName
    Else
        ' Concatenate the drive and file.
        GetLongName = lpPathName & "\" & lpFileName
    End If

End Function

------------
Jim Houghtaling

email:  hmm... no.
web:   working on it (heh)
platform(s):  Win95 os/r2, WinNT 4.0 sp3, VB5ee sp3
('till I get my page up go to VBNet...It's the best I've seen yet.)
http://home.sprynet.com/sprynet/rasanen/vbnet/default.htm
------------
It often shows an excellent command
of language to say nothing.



Quote:
> Thanks,

> I tried the GetShortPathName and GetFullPathName functions right away,
but
> did not have any success with them. The always return a blank string to
me.
> The Daniel Appelman book even says that GetFullPathName is difficult to
use
> in VB, but he doesn't give an example of how to use it right. Do you have
> any experience using these API functions successfully.

> Joe



Sat, 08 Jul 2000 03:00:00 GMT  
 Long File Names and Short File Names

Thanks very much. It's exactly what I was looking for.



Sun, 09 Jul 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Get long file name from short file name

2. Long File Names and Short File Names

3. Long file names to short file name

4. Converting Long File Names to Short File Names

5. long file names to short file name

6. How to convert long file names to short file names

7. Get Short File Name from Long File Name?

8. Getting short file name from long file name:

9. Api function to convert Long File Names To Short Dos File Names

10. Short File Name to Long File Name

11. Want to convert Dir in as long file name to short file name string? How?

12. Want to convert Dir in as long file name to short file name string? How?

 

 
Powered by phpBB® Forum Software