How can I find the short path of a Windows directory/file?

I need to use shortened path names for an application that I am using. For example I need C:\PROGRA~1\ as opposed to C:\Program Files. The program can't handle spaces and won't accept quoted paths (e.g. "C:\Program Files").

If it helps, I am using Windows 7. I can get access to any version since XP, if necessary.


Solution 1:

Start, and type cmd in the run box. Start cmd, and use cd to get to the folder you are interested in:

cd \

Then

dir /x

C:\>dir /x

13/10/2011  09:14 AM    <DIR>          DOCUME~1     Documents and Settings
13/10/2011  09:05 AM    <DIR>          PROGRA~1     Program Files

Solution 2:

Create a bat file in some convenient directory then you could copy+paste the short path from that path.

You could just run command.com and keep doing cd commands to your current directory too.

In Windows batch scripts, %~s1 expands path parameters to short names. Create this batch file:

@ECHO OFF
echo %~s1

I called mine shortNamePath.cmd and call it like this:

C:\> shortNamePath "c:\Program Files (x86)\Android\android-sdk"
c:\PROGRA~2\Android\ANDROI~1

Here's a version that uses the current directory if no parameter was supplied:

@ECHO OFF
if '%1'=='' (%0 .) else echo %~s1

Called without parameters:

C:\Program Files (x86)\Android\android-sdk> shortNamePath
C:\PROGRA~2\Android\ANDROI~1

Using SET and a named variable

Windows Command Prompt has some conventions for handling variables with spaces in their values that are somewhat hard to learn and understand, especially if you have a Unix background.  You can do

SET TESTPATH=c:\Program Files (x86)\Android\android-sdk

(with no quotes), or

SET "TESTPATH=c:\Program Files (x86)\Android\android-sdk"

(note the non-intuitive placement of quotes); then

CALL :testargs "%TESTPATH%"
        ︙

:testargs
echo %~s1
goto :eof