Click to See Complete Forum and Search --> : Only short filenames must be used so I need to convert long filename to short filename


Naveed Anis
February 15th, 2000, 01:02 AM
Please reply immediately cause my task is
on the verge of being lost.

I just want to know; how can we convert long
filenames to short filenames.



A problem stays like a challenge till
I find the solution to it.

Lothar Haensler
February 15th, 2000, 01:18 AM
use the GetShortPathName API!

Naveed Anis
February 15th, 2000, 01:33 AM
I am using it from VB.
Can I get an example please!

A problem stays like a challenge till
I find the solution to it.

Lothar Haensler
February 15th, 2000, 01:39 AM
Option Explicit
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 l as Long
Dim strLong as string * 255
Dim strshort as string * 255
strLong = "c:\winnt\niagnt32_install.log"
l = GetShortPathName(strLong, strshort, len(strLong))
MsgBox strshort
End Sub


tested with NT 4 and VB 6

Naveed Anis
February 15th, 2000, 01:47 AM
I am very very thankful to you.
You have saved my day.
If there was anything more thanks
I could do, I would be obliged to do it.

If I have any other problems, can I contact
you?

if yes then; if you don't have problem with
that; then
please mail me at: naveed_anis_kh@yahoo.com

A problem stays like a challenge till
I find the solution to it.

Lothar Haensler
February 15th, 2000, 01:56 AM
>If I have any other problems, can I contact
you?

the easiest way to contact "me" is by posting your question to this forum.
That way you have access to the knowledge of all other forum participants.

Billkamm
February 22nd, 2005, 11:31 AM
Hi, when using that function it fills the rest of the buffer with NULL characters (maybe the \0 character from c++ not sure).

Is there a VB function to remove the rest of that stuff from the buffer, so I can have just the short path as my string.

Billkamm
February 22nd, 2005, 11:38 AM
Ok I found a way around this using the following function:

Private Function VbNullTrim(strTextToTrim As String) As String
Dim lngPosition As Long ' Position of the NULL character

' Find the position of the first vbNullChar
lngPosition = InStr(strTextToTrim, vbNullChar)

' Trim the vbNullChars off the string
If lngPosition > 0 Then
VbNullTrim = Left$(strTextToTrim, lngPosition - 1)
Else
VbNullTrim = strTextToTrim
End If
End Function

Does anyone know of a built-in function to do this?

Billkamm
February 22nd, 2005, 11:53 AM
I have run into another problem with this code.

I read the documentation at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/getshortpathname.asp

And found out that you shouldn't use len(strLong) as the size of the buffer. For example "c:\a b" converts as "c:\ab~1" which is longer that long filename's length.

The full short path can get quite longer than the long path sometimes (ironic in a way), so it is highly recommended you check to make the return value is not greater than the buffer size you are using

Billkamm
February 22nd, 2005, 12:00 PM
One more note, after I remove the vbNullChar from that buffer I have to Trim() the buffer and assign it to another String.

Cimperiali
February 24th, 2005, 04:54 AM
[...]you shouldn't use len(strLong) as the size of the buffer

Dim strLong as string * 255
means it is a fixed len string
you can fill it with a single char, but it will always be 255 in len
Thus
strLong="a"
msgbox len(strLong) 'you see 255!


Api guide shows this example:

Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Public Function GetShortPath(strFileName As String) As String
'KPD-Team 1999
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRes = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRes)
End Function
Private Sub Form_Load()
MsgBox GetShortPath("c:\Program Files\")
End Sub