Categories
Ξ TREND

How to escape spaces in file paths on the Windows command line

Command line environments like the Windows command prompt and PowerShell use spaces to separate commands and arguments, but file and folder names can also contain spaces. To specify a file path with a space in it, you must “escape”.

Command line 101: Why you have to escape spaces

“Escape” from a character changes its meaning. For example, escaping a space will cause the shell to treat it as a standard space character instead of a special character that separates command line arguments.

For example, suppose you have a text file that you want to see the contents of. You can do this with the type command. Assuming the text file is at C:TestFile.txt, the following command at the command prompt will display its contents:

type C:TestFile.txt

Excellent. Now what if you have the same file in C:Test FolderTest File.txt? If you try to run the following command, it won’t work – those spaces in the file path are getting in the way.

type C: Test Folder Test File.txt

The command line thinks you are trying to find a file called C:Test and says “the specified path cannot be found”.

Three ways to escape spaces in Windows

There are three different ways to escape file paths in Windows:

Enclosing the path (or parts of it) in double quotes (”).
Adding a caret character (^) before each space. (This only works in command prompt/CMD, and doesn’t seem to work with all commands.)
Adding a backtick character (`) before each space. (This only works in PowerShell, but it always works.)

We will show you how to use each method.

Enclose the path in quotation marks («)

The standard way to ensure that Windows treats a file path correctly is to enclose it in double quotes (”). For example, with our sample command above, we would simply run the following:

type “C:Test FolderTest File.txt”

In fact, you can enclose parts of the path in quotes if you prefer. For example, let’s say you have a file called File.txt in that folder. You could run the following:

type C:”Test Folder”File.txt

However, that is not necessary; in most cases, you can use quotes around the entire path.

This solution works in both the traditional Command Prompt (CMD) environment and Windows PowerShell.

Sometimes: use the Caret character to escape spaces (^)

In the command prompt, the caret character (^) will allow you to escape spaces, in theory. Just add it before each space in the file name. (You’ll find this character in the number row on your keyboard. To type the caret, press Shift + 6.)

Here’s the problem: while this should work, and sometimes does, it doesn’t work all the time. The command prompt handling of this character is strange.

For example, with our sample command, you would run the following and it wouldn’t work:

type C:Test^ FolderTest^ File.txt

On the other hand, if we try to open our file directly by typing its path in the command prompt, we can see that the caret escapes spaces correctly:

C:Test^FolderTest^File.txt

So when does it work? Well, based on our research, it seems to work with some apps and not others. Your mileage may vary depending on the command you are using. The command prompt handling of this character is strange. Try it with whatever command you are using, if you are interested it may or may not work.

For consistency, we recommend that you stick to double quotes in the command prompt, or switch to PowerShell and use the backtick method below.

PowerShell: Use the backtick character (`)

PowerShell uses the backtick character (`) as an escape character. Just add it before each space in the file name. (You’ll find this character above the Tab key and below the Esc key on your keyboard.)

type C:Test` FolderTest` File.txt

Each backtick character tells PowerShell to escape the next character.

Note that this only works in the PowerShell environment. You’ll need to use the caret at the command prompt.

If you are familiar with UNIX-like operating systems such as Linux and macOS, you may be used to using the backslash () character before a space to escape. Windows uses this for normal file paths, so it doesn’t work – the accent (^) and backtick (`) characters are Windows’ version of the backslash, depending on the command line shell you’re using.