Article ID: 175512
Article Last Modified on 3/11/2005
APPLIES TO
- Microsoft Visual Basic 5.0 Control Creation Edition
- Microsoft Visual Basic 5.0 Learning Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 5.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
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
This article was previously published under Q175512
SUMMARY
Under certain circumstances, it may be necessary to get the Short Filename equivalent of a file that has a Long Filename. The example below will allow you to select a file that has a Long Filename and return it's Short Filename using the GetShortPathName API Function call.
MORE INFORMATION
The following code example includes a function that will convert long filenames into their short filename equivalents and also includes a simple demonstration of the function's use.
Step-by-Step Example
- Create a new Standard EXE project in Visual Basic. Form1 is created by default.
- Place a CommandButton on Form1.
- Place a Common Dialog control on the form.
- From the Insert menu, select Module to add a single code module to the project.
Add the following code to Module1:
Declare Function GetShortPathName Lib "kernel32" _ Alias "GetShortPathNameA" (ByVal lpszLongPath As String, _ ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long Public Function GetShortName(ByVal sLongFileName As String) As String Dim lRetVal As Long, sShortPathName As String, iLen As Integer 'Set up buffer area for API function call return sShortPathName = Space(255) iLen = Len(sShortPathName) 'Call the function lRetVal = GetShortPathName(sLongFileName, sShortPathName, iLen) 'Strip away unwanted characters. GetShortName = Left(sShortPathName, lRetVal) End Function
Add the following code to Form1:
Private Sub Command1_Click() Dim msg As String CommonDialog1.FileName = "*.*" CommonDialog1.ShowOpen msg = "Long File Name: " & CommonDialog1.filename & vbCrLf msg = msg & "Short File Name: " & GetShortName(CommonDialog1.filename) MsgBox msg End Sub
- Run the project by pressing the F5 key. Click on the Command button to show the Open dialog box. Navigate the Open dialog box and find a file that has a Long Filename. Select the file and click OK.
- The message box will display the Long File name along with its Short File name.
REFERENCES
For additional information, please see the following article in the Microsoft Knowledge Base:
154822 How To Get a Long Filename from a Short Filename
Keywords: kbhowto kbdocfix KB175512