Breaking News
Home / Tips and Tricks / How to escape spaces in file paths on the Windows command line

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

Command line environments such as the Windows command prompt and PowerShell use spaces to separate commands and arguments. However, file and folder names can contain spaces. To specify a file path with a space, you must “mask”

; it.

Command Line 101: Why You Must Escape Spaces

A character’s “escape” changes their meaning. For example, if you escape a space, the shell treats it like a standard space rather than a special character that separates command line arguments.

Suppose you have a text file that you want to view the contents of. You can do this with the type command. Assume the text file is at `C:TestFile.txt`The following command in the command prompt shows the contents:

`type C:TestFile.txt`

Great. What if you have the same file with you? `C:Test FolderTest File.txt`? When you try to run the following command it doesn’t work. These spaces in the file path disturb.

`type C:Test FolderTest File.txt`

The command line thinks you are trying to find a file by that name `C:Test` and says that it “cannot find the path given”.

Three ways to escape spaces on Windows

There are three different ways to bypass file paths on Windows:

• By enclosing the path (or parts of it) in double quotation marks (”).
• By adding a caret (^) before each space. (This only works in Command Prompt / CMD and doesn’t seem to work on every command.)
• By adding a heavy accented character (`) before each space. (This only works in PowerShell, but always.)

We’ll show you how to use the individual methods.

Enclose the path in quotation marks (”)

The standard way to ensure that Windows handles a file path correctly is to enclose it in double quotation marks (”). With our example command above, we would instead run the following instead:

`type "C:Test FolderTest File.txt"`

You can quote parts of the path if you want. Suppose you had a file called File.txt in that folder. You could do the following:

`type C:"Test Folder"File.txt`

However, this is not required. In most cases, you can just put quotation marks around the entire path.

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

Sometimes: use the caret to escape spaces (^)

At the command prompt, you can use the caret (^) to escape spaces – in theory. Just add it before each space in the filename. (You can find this character in the row of numbers on your keyboard. To enter the caret, press Shift + 6.)

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

For example, with our example command, you would do the following and it would not 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 properly leaves the spaces:

`C:Test^ FolderTest^ File.txt`

When does it work Well, based on our research, it seems to work with some applications and not others. Your mileage may vary depending on the command used. The command prompt handling this character is strange. Try the command you use if you’re interested – it may or may not work.

For consistency, we recommend using double quotes in the command prompt – or switching to PowerShell and using the heavily accented method below.

PowerShell: Use the Grave Accent Character (`)

PowerShell uses the heavy accented character (`) as an escape character. Just add it before each space in the filename. (You can find this character above the Tab key and below the Esc key on your keyboard.)

`type C:Test` FolderTest` File.txt`

Each heavily accented character tells PowerShell to escape from the following character.

Note that this only works in the PowerShell environment. You must use the caret in the command prompt.

If you’re familiar with UNIX-like operating systems like Linux and macOS, you may be used to using the forward slash () before a space to bypass it. Windows uses this for normal file paths, so it won’t work. The caret (^) and heavy accent (`) characters are the Windows version of the forward slash, depending on which command-line shell you are using.