Page 1 of 1

Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:00
by MKANET
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.

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:05
by aGerman
What about

Code: Select all

%~dps0

or

Code: Select all

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


Regards
aGerman

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:07
by foxidrive
Just be aware that the ~s short name function has a bug and sometimes returns the wrong info.

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:07
by Fawers
aGerman wrote:

Code: Select all

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


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

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:18
by MKANET
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!

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:26
by Fawers
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.

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 14:42
by aGerman
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

Re: Convert current directory to DOS 8.3-friendly shortname

Posted: 11 Apr 2012 15:06
by MKANET
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