Convert current directory to DOS 8.3-friendly shortname

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Convert current directory to DOS 8.3-friendly shortname

#1 Post by MKANET » 11 Apr 2012 14:00

I need a way to convert the current directory to DOS 8.3 short name.

Currently I call the current directory with %CD%. However, I need the short name equivalent of that.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Convert current directory to DOS 8.3-friendly shortname

#2 Post by aGerman » 11 Apr 2012 14:05

What about

Code: Select all

%~dps0

or

Code: Select all

for %%i in ("%cd%") do echo %%~si


Regards
aGerman

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Convert current directory to DOS 8.3-friendly shortname

#3 Post by foxidrive » 11 Apr 2012 14:07

Just be aware that the ~s short name function has a bug and sometimes returns the wrong info.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Convert current directory to DOS 8.3-friendly shortname

#4 Post by Fawers » 11 Apr 2012 14:07

aGerman wrote:

Code: Select all

for %%i in ("%cd%") do echo %%~s0


Shouldn't it be "%%~si"?
Edit: Nevermind. You edited it already.

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Convert current directory to DOS 8.3-friendly shortname

#5 Post by MKANET » 11 Apr 2012 14:18

Does anyone know under what circumstances this wont work? Is this the best know way to reliably get the current directory as a short name?

Thanks a million!

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Convert current directory to DOS 8.3-friendly shortname

#6 Post by Fawers » 11 Apr 2012 14:26

MKANET wrote:Does anyone know under what circumstances this wont work? Is this the best know way to reliably get the current directory as a short name?

Thanks a million!


Well I've managed to write a script to echo a short path to a .tmp file and start it with notepad. You can then copy the text and, right after you close notepad, the file is deleted.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Convert current directory to DOS 8.3-friendly shortname

#7 Post by aGerman » 11 Apr 2012 14:42

I remember that thread:
http://stackoverflow.com/questions/8354 ... short-name
also
viewtopic.php?f=3&t=2583

Seems to be a bug on XP.

Regards
aGerman

MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Re: Convert current directory to DOS 8.3-friendly shortname

#8 Post by MKANET » 11 Apr 2012 15:06

Currently, I just insert this line of code into my batch files that depend on %CD%

Code: Select all

for %%i in ("%cd%") do set CD=%%~si


I saw a piece of code to make it a bit safer (in the above links). However, not sure how I would incorporate it into my code; or, if it would help any.

Maybe some code like this would make it more robust:

convert using var=%%~sA
:loop
if not exist "%var%\" remove last character and goto :loop
hopefully %var% here will be the short filename

Post Reply