Getshortname

Omar Lujan

New Member
Joined
Nov 4, 2008
Messages
12
Hello,

I've been working with the getshortname() API, but have found that it has trouble handling strings that are already in short name format. I used a trim function to work around this. Now I find another problem; the API doesn't change paths with spaces in them. for example:

H:\WCMGMT\WC Prod\Backup2\

should return

H:\WCMGMT\WCPROD~1\Backup2\

Here is the code:
Code:
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal longPath As String, ByVal shortPath As String, ByVal shortBufferSize As Long) As Long
 
Private Sub Test()
Dim longPathName As String
longPathName = "H:\WCMGMT\WC Prod\Backup2\"
'
'Get the size of the string to pass to the string buffer.
Dim longPathLength As Long
longPathLength = Len(longPathName)
'
'A string with a buffer to receive the short path from the api call...
Dim shortPathName As String
shortPathName = Space$(longPathLength)
'
'Will hold the return value of the api call which should be the length.
Dim returnValue As Long
'Now call the function to do the conversion...
returnValue = GetShortPathName(longPathName, shortPathName, longPathLength)
 
 
ActiveCell = shortPathName
'If shortpathname is all spaces then change the cell txt back to original file path
ActiveCell = Trim(ActiveCell.Text)
If IsEmpty(ActiveCell) = True Then ActiveCell = longPathName

End Sub

The code works, somewhat. It is able to change
C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705

to
C:\WINDOWS\MICROS~1.NET\FRAMEW~1\V10~1.370

but not
H:\WCMGMT\WC Prod\Backup2\
to
H:\WCMGMT\WCPROD~1\Backup2\

as I mentioned above. Does anyone have any suggestions??

Thanks in advance!

-Omar
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi Omar

You are not passing the correct size to the short pathname. You are using the size of the long pathname and that's not what the code expects. You must first query the real size of the short name.

Maybe you can adapt this code that I adapted from ms. Paste this code in a general module:

Code:
[FONT=Arial]Option Explicit[/FONT]
[FONT=Arial][/FONT] 
[FONT=Arial]Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" ( _<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>[/FONT]
[FONT=Arial]            ByVal longPath As String, _<o:p></o:p>[/FONT]
[FONT=Arial]            ByVal shortPath As String, _<o:p></o:p>[/FONT]
[FONT=Arial]            ByVal shortBufferSize As Long _<o:p></o:p>[/FONT]
[FONT=Arial]        ) As Long<o:p></o:p>[/FONT]
[FONT=Arial] <o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]Sub MyGetShortPathName()<o:p></o:p>[/FONT]
[FONT=Arial]    <o:p></o:p>[/FONT]
[FONT=Arial]Dim lLen As Long<o:p></o:p>[/FONT]
[FONT=Arial]Dim sShortPathname As String<o:p></o:p>[/FONT]
[FONT=Arial]Dim sLongPathname As String<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]sLongPathname = "H:\WCMGMT\WC Prod\Backup2\"<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]' First obtain the size needed to the short pathname<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]    lLen = GetShortPathName(sLongPathname, 0, 0)<o:p></o:p>[/FONT]
[FONT=Arial]    If (lLen = 0) Then<o:p></o:p>[/FONT]
[FONT=Arial]        MsgBox "GetShortPathName, error getting space of short pathname"<o:p></o:p>[/FONT]
[FONT=Arial]        Exit Sub<o:p></o:p>[/FONT]
[FONT=Arial]    End If<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]' Dynamically allocate the correct size<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]    sShortPathname = Space$(lLen)<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]' Now simply call again using same long path[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]    lLen = GetShortPathName(sLongPathname, sShortPathname, lLen)<o:p></o:p>[/FONT]
[FONT=Arial]    If (lLen = 0) Then<o:p></o:p>[/FONT]
[FONT=Arial]        MsgBox "GetShortPathName, error getting short pathname"<o:p></o:p>[/FONT]
[FONT=Arial]        Exit Sub<o:p></o:p>[/FONT]
[FONT=Arial]    End If<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]    MsgBox "long name = " & sLongPathname & vbNewLine & _<o:p></o:p>[/FONT]
[FONT=Arial]           "shortname = " & sShortPathname<o:p></o:p>[/FONT]
[FONT=Arial]<o:p> </o:p>[/FONT]
[FONT=Arial]End Sub<o:p></o:p>[/FONT]
 
Upvote 0
Similar to PGC's code:
Code:
Private Declare Function fGetShortPathName Lib "kernel32" Alias "GetShortPathNameA" ( _
            ByVal longPath As String, _
            ByVal shortPath As String, _
            ByVal shortBufferSize As Long _
        ) As Long
 
Sub Test()
  Dim sLongPathName As String
  sLongPathName = Application.Path
  MsgBox "long name = " & sLongPathName & vbNewLine & _
   "shortname = " & GetShortPathName(sLongPathName)
   
  sLongPathName = "c:\Some Path That does not exist"
  MsgBox "long name = " & sLongPathName & vbNewLine & _
   "shortname = " & GetShortPathName(sLongPathName)
End Sub
 
Function GetShortPathName(sLongPathName As String) As String
  Dim lLen As Long
  Dim sShortPathname As String
  If Dir(sLongPathName, vbDirectory) = "" Then Exit Function

  sShortPathname = Space$(260)
 
  lLen = fGetShortPathName(sLongPathName, sShortPathname, 260)
  'If (lLen = 0) Then Exit Function  'Not needed since DIR was used.
  GetShortPathName = sShortPathname
End Function
 
Upvote 0
Thank you both for your replies! Both ways worked for

"H:\WCMGMT\Archive\WC TEST\"

but not for

H:\WCMGMT\Carrier Collections\Carrier Collections\



PGC - In your code, I couldn't get past

Code:
lLen = GetShortPathName(sLongPathname, 0, 0)<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
    If (lLen = 0) Then

When sLongPathname = H:\WCMGMT\Carrier Collections\Carrier Collections\ , ILen = 0 and goes to the error message.


Kenneth - In your code I couldn't get past

Code:
 If Dir(sLongPathName, vbDirectory) = "" Then Exit Function

If I understand it correctly, VB doesn't think that this is a directory?

I also tried commenting out the above and uncommenting

Code:
  If (lLen = 0) Then Exit Function  'Not needed since DIR was used.

But as with PGC's code, lLen returns 0.


Is there a problem when the original path name has more than one space?
 
Upvote 0
Which version of Windows are you using as a matter of interest?
 
Upvote 0
Hi Omar

Just tried the code for:

H:\WCMGMT\Carrier Collections\Carrier Collections\

and had no problem. Got the result:

H:\WCMGMT\CARRIE~1\CARRIE~1\

Make sure you have no extra spaces, both in the vba string and the folder's name. Also make sure they are real spaces and not other characters (sometimes if you copy strings from the web you get the html nbsp character and you think it's a space).
 
Upvote 0
hmmm....It still doesn't seem to work for me. Here's the code

Code:
Option Explicit
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" ( _
            ByVal longPath As String, _
            ByVal shortPath As String, _
            ByVal shortBufferSize As Long _
        ) As Long
 
Sub Test()
Dim lLen As Long
Dim sLongPathName, sShortPathname As String
'''''TEST sLongPathName'''''''
sLongPathName = "H:\WCMGMT\Carrier Collections\Carrier Collections\"
'sLongPathName = "H:\WCMGMT\Archive\WC TEST\"

' First obtain the size needed to the short pathname
    lLen = GetShortPathName(sLongPathName, 0, 0)
    If (lLen = 0) Then
        MsgBox "GetShortPathName, error getting space of short pathname"
        Exit Sub
    End If
'Dynamically allocate the correct size
sShortPathname = Space$(lLen)
' Now call again using same long path
lLen = GetShortPathName(sLongPathName, sShortPathname, lLen)
    If (lLen = 0) Then
        MsgBox "GetShortPathName, error getting short pathname"
        Exit Sub
    End If
MsgBox "long name = " & sLongPathName & vbNewLine & _
           "shortname = " & sShortPathname
           
End Sub

I uncomment the sLongPathName variable for each different try.
H:\WCMGMT\Carrier Collections\Carrier Collections\ still doesn't work.

Thanks for your patience and your help so far

-Omar
 
Upvote 0
Omar

What are you actually trying to achieve?

Are you sure you need to use API?

Have you looked at using Split or perhaps even Dir?
 
Upvote 0
Hi again

I'm afraid I don't know what's your problem. I'm running excel on Vista. I have excel 2007 and excel 2000 and tried your code in both and it worked OK. I don't have access to a machine with XP.

I hope someone else reads this thread, tests the code in XP and post the result.

There may also be other alternatives but I'm leaving now. Hope you solve it soon.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,509
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top