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
 
I use xp and Excel 2003.

Not sure what doesn't work means. PGC's code shows the error message if the path does not exist. Mine returns nothing if the path does not exist.

More than one consecutive space in a folder name is fine.

Copy the path manually and then click Start > Run > paste your folder name > OK. If it opens to that folder then the folder name is valid and exists.
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Does this work using the same path(s):
Code:
Function GetShortName(strPath As String) As String
   Dim fso As Object
   Dim fdr As Object
   Set fso = CreateObject("Scripting.FileSystemObject")
   On Error Resume Next
   Set fdr = fso.GetFolder(strPath)
   If Err.Number <> 0 Then
      GetShortName = "No such folder"
   Else
      GetShortName = fdr.ShortPath
   End If
End Function

Omar, I copied and pasted your code into a module and ran it and it did exactly what I would expect, returning the name correctly (using Windows XP). Are you absolutely sure you have the directory path correct (that's mostly what the code above is to test)?
 
Last edited:
Upvote 0
Rorya and Kenneth - Thank you both. The directory no longer exists. So does the API rely on the the directory to exist? it will return '0' if it doesn't?

Then how was PGC able to get it to work on post #6 of this forum? I don't know enough about it, but I would think that a function would be able to convert the path without the need to check its' existence. Shortname is a MS-dos standard naming convention, so theoretically any windows path could be converted without the need for it to actually exist, right?

Norie - Thanks for your input. I have 2 tables that I want to JOIN in access by path name. One is already in shortname format and the other I'm trying to get into shortname format. I exported the table to excel and have been working with VBA as you tell from the forum. Do you know a better way to approach this?

I have a working solution for my project, but I am curious to know why getshortname() depends on the path to exist?

Thank you all for your help and suggestions.

-Omar
 
Upvote 0
Hi Omar

Then how was PGC able to get it to work on post #6 of this forum?

I created the folder or else the code would not work

Shortname is a MS-dos standard naming convention, so theoretically any windows path could be converted without the need for it to actually exist, right?

No. You know how to build the name. It doesn't mean you know what the result will be.

The short name depends on the other names in the same folder with the same common first characters. That's why there's no way to know the short name if you don't have access to the folder (unless the name has no more than 8 characters).

Do this simple test.

- Open a dos window, create a folder

- create 2 text files abcdefgh1.txt and abcdefgh2.txt. Check their short names:

abcdefgh1.txt ABCDEF~1.txt
abcdefgh2.txt ABCDEF~2.txt

- now delete abcdefgh1.txt. THen create first abcdefgh3.txt and then abcdefgh1.txt. Check the short names:

abcdefgh1.txt ABCDEF~3.txt
abcdefgh2.txt ABCDEF~2.txt
abcdefgh3.txt ABCDEF~1.txt

As you see the fact that you know the rule to build the short names does not mean that you can antecipate the short name of a file. It depends on the order of creation. The file abcdefgh1.txt in this example first has the shortname ABCDEF~1.txt and then ABCDEF~3.txt
 
Upvote 0

Forum statistics

Threads
1,216,471
Messages
6,130,822
Members
449,595
Latest member
jhester2010

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