Short (DOS) names for paths in environment variables

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
R

Rich Pasco

I have an old DOS program which needs the short 8.3 version of a path
named in an environment variable. Is there an easy way to get it?

For example: if %HOMEPATH% is "\Documents and Settings\pasco"
then what yields "\DOCUME~1\PASCO" ?

I know I could write a script to parse the output of "DIR /X" but
I was hoping for an easier way....

- Rich
 
Rich said:
I have an old DOS program which needs the short 8.3 version of a path
named in an environment variable. Is there an easy way to get it?

For example: if %HOMEPATH% is "\Documents and Settings\pasco"
then what yields "\DOCUME~1\PASCO" ?

I know I could write a script to parse the output of "DIR /X" but
I was hoping for an easier way....

Modify this to suit your needs.

@echo off
set LFN=\Documents and Settings\pasco
echo> tmp.vbs s = Wscript.Arguments(0)
echo>>tmp.vbs Set fso = CreateObject("Scripting.FileSystemObject")
echo>>tmp.vbs If fso.FolderExists(s) Then
echo>>tmp.vbs Wscript.Echo "set sfn=" + fso.GetFolder(s).ShortPath
echo>>tmp.vbs ElseIf fso.FileExists(s) Then
echo>>tmp.vbs Wscript.Echo "set sfn=" + fso.GetFile(s).ShortPath
echo>>tmp.vbs End If
cscript /nologo tmp.vbs "%lfn%" >tmp.bat
call tmp.bat
del tmp.bat
del tmp.vbs
echo SFN=%sfn%
 
Todd said:
Modify this to suit your needs.

@echo off
set LFN=\Documents and Settings\pasco
echo> tmp.vbs s = Wscript.Arguments(0)
echo>>tmp.vbs Set fso = CreateObject("Scripting.FileSystemObject")
echo>>tmp.vbs If fso.FolderExists(s) Then
echo>>tmp.vbs Wscript.Echo "set sfn=" + fso.GetFolder(s).ShortPath
echo>>tmp.vbs ElseIf fso.FileExists(s) Then
echo>>tmp.vbs Wscript.Echo "set sfn=" + fso.GetFile(s).ShortPath
echo>>tmp.vbs End If
cscript /nologo tmp.vbs "%lfn%" >tmp.bat
call tmp.bat
del tmp.bat
del tmp.vbs
echo SFN=%sfn%

Thanks, Todd.

Also, it seems that Perl has it built-in as
Win32::GetShortPathName(PATHNAME)

- Rich
 
Rich said:
Also, it seems that Perl has it built-in as
Win32::GetShortPathName(PATHNAME)
And pure batch :

for %%A in ("%HOMEPDRIVE%%HOMEPATH%") do set HOMEPATH=%%~spnxA
 
Matthias said:
And pure batch :

for %%A in ("%HOMEPDRIVE%%HOMEPATH%") do set HOMEPATH=%%~spnxA

Or just:

for %%A in ("%HOMEPDRIVE%%HOMEPATH%") do set HP=%%~sA
echo %HP%

Note: I chose HP so as not to overwrite HOMEPATH.

- Rich
 
Matthias said:
And pure batch :

for %%A in ("%HOMEPDRIVE%%HOMEPATH%") do set HOMEPATH=%%~spnxA

IIRC, the ~s does not work correctly 100% of the time.

Was the ~s fixed to work 100% correctly?
If so, where can the patch be downloaded?
 
Rich Pasco said:
I have an old DOS program which needs the short 8.3 version of a path
named in an environment variable. Is there an easy way to get it?

For example: if %HOMEPATH% is "\Documents and Settings\pasco"
then what yields "\DOCUME~1\PASCO" ?

I know I could write a script to parse the output of "DIR /X" but
I was hoping for an easier way....

Hi,
I am not sure whether you need this to work under Win2k or XP.
I can only cite the XP help - "Use of batch parameters".

Put the call to your DOS program in a batch file (e.g. test.bat)
and instead of %1 use the notation %~s1
then call the batch file with the environment variable in double quotes
e.g.
test "%homepath%"

Hope this works under Win2k, too.


--
email me: change "nospam" to "w.hennings"
Wilfried Hennings c./o.
Forschungszentrum (Research Center) Juelich GmbH, MUT
<http://www.fz-juelich.de/mut/mut_home>
All opinions mentioned are strictly my own, not my employer's.
 
Wilfried said:
Hi,
I am not sure whether you need this to work under Win2k or XP.
I can only cite the XP help - "Use of batch parameters".

Put the call to your DOS program in a batch file (e.g. test.bat)
and instead of %1 use the notation %~s1
then call the batch file with the environment variable in double quotes
e.g.
test "%homepath%"

Hope this works under Win2k, too.

Can only be relied upon if Microsoft provides a patched cmd.exe so ~s works
correctly 100% of the time.
 
Todd said:
Can only be relied upon if Microsoft provides a patched cmd.exe so ~s works
correctly 100% of the time.

Todd, %~sX works for me.

Where can I learn more about the bug you mentioned? Has it been posted
anywhere?

Here is an interesting page I found while searching...
http://www.ss64.com/ntsyntax/parameters.html
but the issues it describes don't sound as serious as the bug you mention.

- Rich
 
Todd said:
IIRC, the ~s does not work correctly 100% of the time.

Was the ~s fixed to work 100% correctly?
If so, where can the patch be downloaded?
As far as I remember the issue was with repeatedly using the ~s with
folders/subfolders/files.
 
Rich said:
Or just:

for %%A in ("%HOMEPDRIVE%%HOMEPATH%") do set HP=%%~sA
echo %HP%

Note: I chose HP so as not to overwrite HOMEPATH.

As the change is only temporarily to the current cmd, there should be no
harm changing to the short path as it is still functional for later used
programs. (You could also do a setlocal, endlocal)
 
Todd, %~sX works for me.

Where can I learn more about the bug you mentioned? Has it been posted
anywhere?

Yes, in this group in the past. %~sI works most of the time, but you can't
rely upon it.
 
foxidrive said:
Yes, in this group in the past. %~sI works most of the time, but you can't
rely upon it.
A simple workaround for this case:

pushd "%HOMEDRIVE%%HOMEPATH%"
edlin >NUL 2>&1
set HP=%CD:~2%
popd
 
Rich Pasco said:
Todd, %~sX works for me.

Where can I learn more about the bug you mentioned? Has it been posted
anywhere?

Here is an interesting page I found while searching...
http://www.ss64.com/ntsyntax/parameters.html
but the issues it describes don't sound as serious as the bug you mention.

I only know it is unreliable, therefore I don't use it nor recommend it to
others so as to not be bitten_by_a_bug.

Note, the page above incorrectly labels a caveat of letter usage as a bug.
It does not refer to any actual bugs. Perhaps the author of this page (Simon
Sheppard) will change the word "BUG" to "CAVEAT" as it should be properly
noted. In addition, perhaps the exact circumstance which triggers the ~s bug
I mention could be added to the page if someone knows it and knows how to
contact Simon Sheppard.
 
Matthias said:
As far as I remember the issue was with repeatedly using the ~s with
folders/subfolders/files.

I don't know the exact trigger and I don't recall it ever was posted
anywhere, but I can open a fresh cmd window and reproduce the error with
only a single file spec. It seems to be related to the number of characters
in the path plus some other factor but I don't have time to research it more
fully.

The issue is spurious characters are appended to the short name.

i.e. for say "C:\My Documments\paper 3.doc" the short spec is
C:\MYDOC~1\PAPER3~1.DOC but %~s1 returns C:\MYDOC~1\PAPER3~1.DOCdoc

In other words the problem is evident when the first spec is 10 or more
characters, but I have not figured out the exact trigger in the second spec
for the bug to bite.
 
Todd said:
I only know it is unreliable, therefore I don't use it nor recommend it to
others so as to not be bitten_by_a_bug.

Note, the page above incorrectly labels a caveat of letter usage as a bug.
It does not refer to any actual bugs. Perhaps the author of this page (Simon
Sheppard) will change the word "BUG" to "CAVEAT" as it should be properly
noted. In addition, perhaps the exact circumstance which triggers the ~s bug
I mention could be added to the page if someone knows it and knows how to
contact Simon Sheppard.

Hello all, I looked through a few previous threads and found this:

http://groups.google.co.uk/group/al...+short+name+bug&rnum=2&hl=en#ad56eaf335659c3b

I think whats happening is the ~s option fails when the current
directory is not the same as the 8.3 version of the directory

e.g. if the current directory is C:\Program Files\ you will see the bug
if the current directory is C:\progra~1\ it will work fine (but then you
wont see the long name)

The workaround in the thread is to start and exit command.com, which
changes the current directory to 8.3

I'll ammend the page above with a note on this

Thanks

Simon Sheppard
-
 
Simon said:
Hello all, I looked through a few previous threads and found this:

http://groups.google.co.uk/group/al...+short+name+bug&rnum=2&hl=en#ad56eaf335659c3b

I think whats happening is the ~s option fails when the current
directory is not the same as the 8.3 version of the directory

e.g. if the current directory is C:\Program Files\ you will see the bug
if the current directory is C:\progra~1\ it will work fine (but then you
wont see the long name)

The workaround in the thread is to start and exit command.com, which
changes the current directory to 8.3

I'll ammend the page above with a note on this

Thanks

Simon Sheppard

Excellent memory!

That discussion was about two bugs to watch out for:

The one where the ~f does not expand the LFN when a 8.3 spec is provided,
and the ~s discussed in this thread.

Thank you for keeping it in good spirit and for updating your page Simon.
 
Back
Top