How To Get a Long Filename from a Short Filename

This article was previously published under Q154822
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
The Dir() function can be used to return a long filename but it does notinclude path information. By parsing a given short path/filename into itsconstituent directories, you can use the Dir() function to build a longpath/filename. This article demonstrates how to accomplish this behavior.
MORE INFORMATION
The following code example includes a function that will convert shortfilenames into their long filename equivalents and also includes a simpledemonstration of the function's use.

Step-by-Step Example

  1. Start Visual Basic. Form1 is created by default.
  2. Place a Command button onto Form1.
  3. From the Insert menu, select Module to add a single code module to the project.
  4. Add the following code to Module1:
       Public Function GetLongFilename _     (ByVal sShortName As String) _     As String     Dim sLongName As String     Dim sTemp As String     Dim iSlashPos As Integer     'Add \ to short name to prevent Instr from failing     sShortName = sShortName & "\"     'Start from 4 to ignore the "[Drive Letter]:\" characters     iSlashPos = InStr(4, sShortName, "\")     'Pull out each string between \ character for conversion     While iSlashPos       sTemp = Dir(Left$(sShortName, iSlashPos - 1), _         vbNormal + vbHidden +    vbSystem + vbDirectory)       If sTemp = "" Then         'Error 52 - Bad File Name or Number         GetLongFilename = ""         Exit Function       End If       sLongName = sLongName & "\" & sTemp       iSlashPos = InStr(iSlashPos + 1, sShortName, "\")     Wend     'Prefix with the drive letter     GetLongFilename = Left$(sShortName, 2) & sLongName   End Function					
  5. Add the following to the Command1_Click event:
       Private Sub Command1_Click()     'Assumes C:\Program Files\Common Files is a valid path     Print GetLongFilename("C:\PROGRA~1\COMMON~1")   End Sub					
  6. Choose Start from the Run menu, or press the F5 key to start.
  7. Click the Command button.
If the path you have supplied is valid, the long equivalent will beprinted on the form. If it is not valid, nothing will print, in which caseyou will need to supply a valid short filename in the Command1_Click eventcode.
kbVBp400 vkbVBp500bug kbVBp600bug kbVBp kbdsd kbDSupport KBWIN32SDK KBAPI
Properties

Article ID: 154822 - Last Review: 02/27/2014 10:27:43 - Revision: 4.1

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • kbnosurvey kbarchive kbhowto KB154822
Feedback