122

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.

Nathan
  • 1,385
  • 5
  • 19
  • 36
somehume
  • 1,357

12 Answers12

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
Paul
  • 61,193
69

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
SSpoke
  • 791
32

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:

phuclv
  • 30,396
  • 15
  • 136
  • 260
Ivan Schwarz
  • 429
  • 5
  • 6
20

I found very handy way to solve short pathname of current directory (or anything else) if you have Powershell installed.

Just open powershell in current dir

  • in cmd windows type powershell

  • if you have folder open in gui you can type cmd.exe or powershell.exe directly in address bar of folder.

Then give command

(New-Object -ComObject Scripting.FileSystemObject).GetFolder(".").ShortPath

Origin of information: [https://gallery.technet.microsoft.com/scriptcenter/Get-ShortName-90a49303]

Eino Mäkitalo
  • 389
  • 3
  • 3
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
phuclv
  • 30,396
  • 15
  • 136
  • 260
7

Similar to Ivan Schwartz's answer, you can replace "C:\Program Files" with %cd% to get current dir:

cmd /c for %A in ("%cd%") do @echo %~sA  
bertieb
  • 7,543
Eino Mäkitalo
  • 389
  • 3
  • 3
4
C:\Users\abcd>subst z: "c:\Program Files (x86)\Microsoft Office365 Tools\Microsoft Visual Studio 14.0"

C:\Users\abcd>subst
Z:\: => C:\Program Files (x86)\Microsoft Office365 Tools\Microsoft Visual Studio 14.0"

This is the easiest way I have used when dealing with files with spaces and this is accessible from file explorer too has all the same access privileges.

phuclv
  • 30,396
  • 15
  • 136
  • 260
4

for windows 10 users NONE of these solutions was working for on my windows 10 laptop since a key in the registry prohibited the creation of shortnames.. :

  • Alt+r, type regedit to open the registry editor
  • go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem
  • look for the NtfsDisable8dot3NameCreation key : if the value is 0x00000001(1), then your system does not create any shortname for your folder/file
  • in this case double clic on the NtfsDisable8dot3NameCreation key,
  • type 0 in the data field

this is not retroactive :) I mean you need to recreate the folder/file you need to access from your old app.. maybe something clever is possible though I don't know yet.

it seems they introduced this key to speed up filesystem operations.

source microsoft : https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-2000-server/cc959352(v=technet.10)?redirectedfrom=MSDN

jerome
  • 49
3

Alternatively, you could use this awesome little tool called PathCopyCopy

In a few clicks, you can get the long and short path of literally any folder from the contextual menu, e.g:

Right-click in the destination folder => Path Copy => Short Path.

Done. It will be copied to your clipboard.

preview

Asamoah
  • 131
1
@echo off
for %%i in (%*) do echo %%~si
pause

Save it as shortpath.bat , and then drag files on it. Its result:

C:\PROGRA~1
C:\PROGRA~2
C:\PROGRA~3
C:\Python27
C:\Users
C:\Windows
Press any key to continue . . .

Calling it with shortpath <file path> is also OK.

NeoBlackXT
  • 301
  • 1
  • 4
-1

Just replace the spaces with

%20

It's the way things are "translated", and spaces go into %20.

If you really need alot, simply pop open your browser and type something like

test ";($#< and find the word test, and see that the space is %20 and so on...
-1

I have installed node modules by running npm install on a boilerplate. While trying to delete those folders, windows does not allow us to delete them as path is too long to be able to handle.

After some of shallow research, I thought it would be right my own piece of code snippet to rename the folders from root to leaf so that it would throw any violation exception for this attempt as well.

It works for me. Following is the code for the C# project.

    public static int directoryCounterIndex = 0;
    public static void Main(string[] args)
    {
        string dirPath = @"D:\Studies\MeanStack\a\nodem";
        RenameDirectories(dirPath);
    }

    private static void RenameDirectories(string dirPath)
    {
        directoryCounterIndex += 1;
        var newPath = Path.GetDirectoryName(dirPath) + Path.DirectorySeparatorChar + directoryCounterIndex.ToString();
        Directory.Move(dirPath, newPath);
        var subDirectories = Directory.GetDirectories(newPath);
        foreach (var subDirectory in subDirectories)
        {
            RenameDirectories(subDirectory);
        }
    }
phuclv
  • 30,396
  • 15
  • 136
  • 260
Ashok
  • 9