The Computer Oracle

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

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping

--

Chapters
00:00 How Can I Find The Short Path Of A Windows Directory/File?
00:27 Accepted Answer Score 111
00:45 Answer 2 Score 11
01:07 Answer 3 Score 68
02:05 Answer 4 Score 31
02:39 Thank you

--

Full question
https://superuser.com/questions/348079/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#windows7

#avk47



ACCEPTED ANSWER

Score 111


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



ANSWER 2

Score 68


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



ANSWER 3

Score 31


Here is a one liner:

cmd /c for %A in ("C:\Program Files") do @echo %~sA

Breakdown:

  • cmd /c - Starts a new instance of the Windows command interpreter, carries out the command specified by string and then terminates
  • for %%parameter in (set) do command - Conditionally perform a command several times.
  • echo - Display messages on screen. @ symbol is the same as ECHO OFF applied to the current line only.
  • %~s - Expanded path contains short names only.

Sources:




ANSWER 4

Score 11


The "short name" is really the old DOS 8.3 naming convention, so all the directories will be the first 6 letters followed by ~1 assuming there is only one name that matches, for example:

C:\ABCDEF~1    - C:\ABCDEFG I AM DIRECTORY
C:\BCDEFG~1    - C:\BCDEFGHIJKL M Another Directory

here is the only exception

C:\ABCDEF~1    - C:\ABCDEFG I AM DIRECTORY
C:\ABCDEF~2    - C:\ABCDEFGHI Directory as well