قالب وردپرس درنا توس
Home / Tips and Tricks / How to avoid spaces in file paths on the Windows command line

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

Command Prompt Window in Windows 10

Command line environments such as 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 inside, you must “escape”

; it.

Command line 101: Why you need to escape space

“Escape” a character changes its meaning. For example, if you avoid a space, the shell will treat it as a default character instead of a special character that separates command line arguments.

For example, let’s say you have a text file that you want to see the contents of. You can do that with the type command. Provided the text file is on C:TestFile.txtthe following command in the command prompt will display its contents:

type C:TestFile.txt

Good. Now think if you have the same file on C:Test FolderTest File.txt? If you try to run the command below it will not work – these file path spaces get in the way.

type C:Test FolderTest File.txt

The command line thinks you are trying to search for a file named C:Test and says that it “does not find the specified path.”

Command interpreting error when not accommodating spaces

Three ways to escape spaces on Windows

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

  • By closing the path (or parts of it) in double quotation marks (“).
  • By adding a caret character (^) before each space. (This only works in Command Prompt / CMD, and it does not seem to work with all commands.)
  • By adding a serious accent character (`) before each space. (This only works in PowerShell, but it always works.)

We show you how to use each method.

Attach the path in quotation marks (“)

The usual way to ensure that Windows handles a file path properly is to attach it with double quotes (“). For example, with our example command above, we would just run the following instead:

type "C:Test FolderTest File.txt"

You can actually attach parts of the course in quotation marks if you prefer. For example, let’s say you had a file named File.txt in that folder. You can run the following:

type C:"Test Folder"File.txt

However, it is not necessary – in most cases you can only use quotation marks around the entire course.

This solution works both in the traditional command prompt (CMD) and in Windows PowerShell.

Enclose spaces in double quotes in the command prompt

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

In the command interpreter, the guard character (^) will let you escape from spaces – in theory. Just add it before each space in the file name. (You will find this character in the number row on the keyboard. To type the caret character, press Shift + 6.)

Here’s the problem: While this should work, and it does work sometimes, it does not work all the time. Command interpreter’s handling of this character is strange.

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

type C:Test^ FolderTest^ File.txt

Command Prompt Error Space Error

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 guard character avoids spaces properly:

C:Test^ FolderTest^ File.txt

Space that does not work in the command prompt

So 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 you use. Command interpreter’s handling of this character is strange. Try which command you use, if you are interested – it may or may not work.

For consistency, we recommend that you keep double quotes in the command prompt – or switch to PowerShell and use the serious accent method below.

PowerShell: Use Grave Accent Character (`)

PowerShell uses the bold accent character (`) as its escape character. Just add it before each space in the file name. (You can find this character above the Tab key and below the Esc key on your keyboard.)

type C:Test` FolderTest` File.txt

Each serious character tells PowerShell to escape from the following character.

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

Holds spaces with grave accents in PowerShell

If you are familiar with UNIX-like operating systems such as Linux and macOS, you may be used to using backslash () before a space to escape it. Windows uses this for normal file paths, so it does not work —- caret (^) and grave accent (`) are the Windows version of backspace, depending on which command line shell you use.

Source link