Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find out a file's ms-dos name

Status
Not open for further replies.

crrrazydan

Programmer
Jul 27, 2000
34
US
How do you find out in VB, what a file's ms-dos name is? For example if I had selected File1.filename, which could be say, "Metallica - One.mp3" then how would I find out that it's ms-dos name is "Metall~2.mp3"? Thanks
 
By way of an API call.....

Include a BAS module in your project and place the following code inside it:

Option Explicit

Public 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


and then to retrieve the DOS short name, call the function GetShortName:

Dim sShortName As String

sShortName = GetShortName("C:\program files\")

Simon
 

Another way to do this is by using the "Microsoft Scripting Runtime" library. Just make a reference to it and the code below should work:

Dim fs As Scripting.FileSystemObject
Dim fl As Scripting.Folder
Dim f As Scripting.File

Set fs = CreateObject("Scripting.FileSystemObject")
Set fl = fs.GetFolder("your directory path")
Set f = fl.Files("your file")
MsgBox f.ShortName

The Scripting library provides lots of properties and methods on files and folders. It is a valuable tool.

P.S. Simon's solution works very nicely!

Tarek

The more I learn, the more I need to learn!
 
This got me thinking about the two methods.
Personally I do not like using VBScript in compiled VB applications, so I was wandering which of the two methods would be quicker to run.

I ran the two lots of code:

'''''''''' first code using API call ''''''''''''''
Dim sFileName As String
Dim sShortFileName As String
Dim i As Long
Dim objComdlg As New CommonDialog
Dim dtStart As Date
Dim dtEnd As Date

objComdlg.Flags = cdlOFNExplorer
objComdlg.Filter = "All Files(*.*)|*.*"
objComdlg.CancelError = False
objComdlg.ShowOpen
If Len(objComdlg.FileName &amp; &quot;&quot;) <> 0 Then
sFileName = objComdlg.FileName
End If
Set objComdlg = Nothing

' begin the loop to call the API
If Len(sFileName &amp; &quot;&quot;) <> 0 Then
dtStart = Now()
For i = 1 To 20000
sShortFileName = GetShortName(sFileName)
Next i
dtEnd = Now()
MsgBox &quot;Time Taken = &quot; &amp; DateDiff(&quot;s&quot;, dtStart, dtEnd)
End If


'''''''''' second code using scripting ''''''''''''''
Dim fs As Scripting.FileSystemObject
Dim f As Scripting.File
Dim objComDlg As New CommonDialog
Dim sFileName As String
Dim sshortfilename As String
Dim dtStart As Date
Dim dtEnd As Date
Dim i As Long

objComDlg.Flags = cdlOFNExplorer
objComDlg.Filter = &quot;All Files(*.*)|*.*&quot;
objComDlg.CancelError = False
objComDlg.ShowOpen
If Len(objComDlg.FileName &amp; &quot;&quot;) <> 0 Then
sFileName = objComDlg.FileName
End If
Set objComDlg = Nothing

' begin the loop to call the API
If Len(sFileName &amp; &quot;&quot;) <> 0 Then
dtStart = Now()

'''''''''''''''''''''''''''''''''''''''''''''''''
'''' BEWARE - this takes approx 68 seconds ''''
'''''''''''''''''''''''''''''''''''''''''''''''''

For i = 1 To 20000
Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f = fs.GetFile(sFileName)
sshortfilename = f.ShortName
Set f = Nothing
Set fs = Nothing
Next i
dtEnd = Now()
MsgBox &quot;Time Taken = &quot; &amp; DateDiff(&quot;s&quot;, dtStart, dtEnd)
End If



And the results (of the Luxembourg judges) are:

VBScript; nil points
API Call; 12 points

The actual times involved were:
VBScript; 68 seconds
API Call; 12 seconds - over 5 times as fast (presumably due to having to create the file system object,.. when using VBScript)
Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top