Spaces in file paths windows

How to enter file or folder paths with spaces in Command Prompt on Windows 10

Dec 6, 2019
Comment

We share a lot of tips and tricks that involve running commands in Command Prompt on Windows 10. A lot of common things, such as pinging a server, or checking the status of your network switch are done vie Command Prompt. If you’re not comfortable using the Command Prompt beyond commands that are already written out and to be executed as they are, you tend to miss out on lots of useful things you can do from the Command Prompt. One, rather frequent question that new users have when using the Command Prompt is how to enter the name or address of a folder or file that has a space in its name or in its path.

Generally speaking, if you’re trying to run a command that involves specifying the path to a folder or file, and the path is incorrect i.e., Command Prompt is unable to see it, the error message won’t tell you as much. The message that Command Prompt returns will vary depending on the command you’ve run and it will seem more like there’s something wrong with the command, rather than the path making it more difficult to trouble shoot the problem. The fix is really simple.

Entering paths with spaces

The trick is the double-quotes. Make it a rule of thumb to enclose any and all file paths that you enter in Command Prompt in double quotes.

The following command will not run. The path has a space in it and at that space, the command breaks and Command Prompt thinks you’ve entered a new command or parameter.

This command will work. The only difference between the two is that in the second one, the path is in double-quotes.

Even if your path doesn’t have a space in it, it’s a good idea to enclose it in double-quotes and develop the habit of doing it. If you forget, or you’re dealing with a longer path, a simple error like this might be hard to spot.

This holds true for all command line apps that you use on Windows 10. In PowerShell, any command that requires a file or folder path to be entered should be enclosed in double-quotes. If the path doesn’t have a space in it, you’ll be fine but if it does, the command won’t run so again, this is about developing a habit to save yourself trouble later.

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How do I use spaces in the Command Prompt?

How can I use spaces in the Windows Command Line?

11 Answers 11

Single quotation marks won’t do in that case. You have to add quotation marks around each path and also enclose the whole command in quotation marks:

I just figured out that for a case where the path involves the use of white space characters, for example, when I need to access the app xyz which location is :

To run this from windows cmd prompt, you need to use

If double quotes do not solve the issue then try e.g.

to get a list of alternative file or directory names. Example output:

Now use the short 8 character file or folder name in the 5th column, e.g. PROGRA

1.XML, in your commands. For instance:

Enclose the paths containing spaces with double quotes.

I prefer to enclose the command in () which is valid batch which makes it a bit easier to read:

Try to provide complex pathnames in double-quotes (and include file extensions at the end for files.)

Читайте также:  Adobe upgrade mac to windows

CMD interprets text with double quotes («xyz») as one string and text within single quotes (‘xyz’) as a command. For example:

FOR %%A in (‘dir /b /s *.txt’) do (echo «%%A»)

And one good thing, cmd is not* case sensitive like bash. So «New fiLE.txt» and «new file.TXT» is alike to it.

*Note: The %%A variables in above case is case-sensitive (%%A not equal to %%a).

this worked for me in a batch file

Just add Quotation Mark

Example:«C:\Users\User Name»

Hope it got Solved!

You should try using quotes.

Spaces in the Commend Prompt (in a VBA Shell command code line)

I had a very similar problem which ended up being a space in the command prompt when automating via VBA to get the contents from the command window into a text file. This Thread was one of many I caught along the way that didn’t quite get me the solution.

So this may help others with a similar problem: Since the syntax with quotes is always difficult to get right , I think showing some specific examples is always useful. The additional problem you get using the command prompt in VBA via the Shell thing, is that the code line often won’t error when something goes wrong: in fact a blink of the black commend window misleads into thinking something was done.

As example… say I have a Folder, with a text file in it like at

The space there in the folder name gives the problem.

Something like this would work, assuming the Folder, AlansFolder, exists

This won’t work. (It won’t error).

Including quote pairs around the path will make it work

( By the way, if the text file does not exist, then it will be made).

With the benefit of hindsight, we can see that my solution does tie up approximately with some already given..

Converting that code line to a manual given command we would have

That seems to work

This works also

This final form also works and ties up with the solution from sacra ….” You have to add quotation marks around each path and also enclose the whole command in quotation marks “ …..

Spaces and Parenthesis in windows PATH variable screws up batch files

So, my path variable (System->Adv Settings->Env Vars->System->PATH) is set to:

Although, obviously, without the newlines.

Notice the lines containing PathWithSpaces — the first has no spaces, the second has a space, and the third has a space followed by a parenthesis.

Now, notice the output of this batch file:

or specifically the line:

So, what is this bullshit?

  • Directory in path that is properly escaped with quotes, but with no spaces = fine
  • Directory in path that is properly escaped with quotes, and has spaces but no parenthesis = fine
  • Directory in path that is properly escaped with quotes, and has spaces and has a parenthesis = ERROR

Whats going on here? How can I fix this? I’ll probably resort to a junction point to let my tools still work as workaround, but if you have any insight into this, please let me know 🙂

8 Answers 8

This can happen if there are unescaped parentheses in a line inside a «block» (which also uses parentheses for delimiting).

You can usually fix it by turning on delayed expansion and use variables with !var! instead of %var% . There isn’t much more advice I could give without seeing the code.

Note for Windows users on 64-bit systems

1 = ‘Program Files’ Progra

2 = ‘Program Files(x86)’

There should either (a) not be any quotes in the MS-Windows PATH environmental variable (PATH command) or (b) there should be quotes surrounding the entire expression following the (SET command). Unfortunately, this is not very well documented by MS, although they do state that if quotes are used, they will be included in the value of the variable (Windows XP Command Line Reference).

This can cause problems that are inconsistent and therefore difficult to diagnose. For example if your path includes «C:\Python27», your machine will say «‘python’ is not recognized as an internal or external command, operable program or batch file.» when you try to execute python. However some libraries may still be available.

Читайте также:  Стабильность windows linux macos

You do not need to «escape» spaces or parentheses. If you need to escape special characters, then put quotes around the entire expression, including the variable name.

or you can use parentheses too.

Note, double quotes must come in pairs.

However, there probably are not any characters that are valid pathnames, that would cause a problem with the SET command.

The solution they suggest is to use delayed expansion.

For setting a path in an if block, rather than using SET PATH= , you should probably use the PATH command.

For other variables, another solution may be to use quotes, but around the whole thing:

Joey in his answer says

This can happen if there are unescaped parentheses in a line inside a «block» (which also uses parentheses for delimiting).

and that’s true. If there are unescaped parentheses one should, well escape them. That’s what I did; I replaced

and this solved the problem.

I’ve experienced something similar. Microsoft explains the issue here: http://support.microsoft.com/kb/329308

Basically, instead of changing the Path variable via System->Adv Settings->Env Vars->System->PATH, try

In Windows 8 I’ve found very little success with any of these methods. Parentheses do not work, quotes work, but the «path» you modify this way isn’t the path that gets used for locating executables, instead cmd still seems to be using the system path it inherited when you opened the window.

example: after determining the processor architecture, I want to add a couple of paths to the PATH environment variable. Actually, even just adding them temporarily would work since I only need them while a batch file is running. But that doesn’t even work.

echo %path% displays the system PATH at the time the cmd was launched.

set path=»%path%;%programfiles(x86)%\company\program\subdir» works but now %path% contains everything surrounded by quotes, and if I try to run a program in subdir from somewhere else, it fails. Using parentheses around the whole thing instead of quotes does not work.

Another thing I’ve noticed is that the same command will work if entered interactively in cmd , but not if encountered in a batch file. That’s frightening. Yet another oddity is the intermittent loss of the last character of an environment variable’s value! Another inconsistency is with third party programs: some can handle a %var% as a parameter, others don’t.

How do I specify C:\Program Files without a space in it for programs that can’t handle spaces in file paths?

A configuration file needs position of another file,

but that file is located in «C:\Program Files»,

and the path with space in it is not recognized,

Is there another way to specify the location without space in it?

14 Answers 14

you should be able to use

  • «c:\Program Files» (note the quotes)
  • c:\PROGRA

1 (the short name notation)

Try c:\> dir /x (in dos shell)

This displays the short names generated for non-8dot3 file names. The format is that of /N with the short name inserted before the long name. If no short name is present, blanks are displayed in its place.

1″ means the same as «c:\Program Files»? – omg May 21 ’09 at 12:01

2 for «C:\Program Files» – Dirk Vollmar May 21 ’09 at 12:05

Never hardcode this location. Use the environment variables %ProgramFiles% or %ProgramFiles(x86)% .

When specifying these, always quote because Microsoft may have put spaces or other special characters in them.

In addition, the directory might be expressed in a language you do not know. http://www.samlogic.net/articles/program-files-folder-different-languages.htm

Use these commands to find the values on a machine. DO NOT hardcode them into a program or .bat or .cmd file script. Use the variable.

Use the following notations:

    For «C:\Program Files«, use «C:\PROGRA

1»
For «C:\Program Files (x86)«, use «C:\PROGRA

Thanks @lit for your ideal answer in below comment:

Use the environment variables %ProgramFiles% and %ProgramFiles(x86)%

2 respectively. If someone has installed windows onto a system that already had a folder called, for example, «C:\Programmers», then that would take up the short name «C:\PROGRA

1″ so «C:\Program Files» would be «C:\PROGRA

2″, etc. – Eureka Dec 27 ’18 at 13:30

I think the reason those suggesting using the C:\PROGRA

1 name have received downvotes is because those names are seen as a legacy feature of Windows best forgotten, which may also be unstable, at least between different installations, although probably not on the same machine.

Also, as someone pointed out in a comment to another answer, Windows can be configured not to have the 8.3 legacy names in the filesystem at all.

The Windows shell (assuming you’re using CMD.exe) uses %ProgramFiles% to point to the Program Files folder, no matter where it is. Since the default Windows file opener accounts for environment variables like this, if the program was well-written, it should support this.

Also, it could be worth using relative addresses. If the program you’re using is installed correctly, it should already be in the Program Files folder, so you could just refer to the configuration file as .\config_file.txt if its in the same directory as the program, or ..\other_program\config_file.txt if its in a directory different than the other program. This would apply not only on Windows but on almost every modern operating system, and will work properly if you have the «Start In» box properly set, or you run it directly from its folder.

There should be a way to use the full c:\program files path directly. Often, it involves encapulating the string in quotes. For instance, on the windows command line;

will not start Internet Explorer, but

Sometimes you can quote the filename.

Some programs will tolerate the quotes. Since you didn’t provide any specific program, it’s impossible to tell if quotes will work for you.

setx setting PATH with spaces

I am trying to update the system Path variable in a win32 shell script but only if a value is not present.

My current version looks something like this:

The problem I am having is with

This doesn’t work, but if I change the quotes to single quote

It does work but the Path ends at the first occurrence of Program Files. I.e abc;def;Program

Is there any way to overcome this?

7 Answers 7

Actually there is an easier way.. it seems to work but it is somewhat ugly and non-intuitive:

Notice that there is ONLY one double-quote and it is at the beginning .. if you put one at the end, it ends up in the path string (without the one at the beginning).

dp0 (with spaces in the .bat path) – Remi Arnaud Mar 15 ’17 at 23:18

I am a bit late but I was looking for something similar and was unable to find it so I figured it out on my own. I was trying to set variable to launch VSC# from Command Line and the exe file for VSC# was present at a path containg space similar to your case. So the solution for me was to set variable using the following:

Single quotes never work in Windows. Period.

Your code does work for me with the double quotes, actually.

I found this question because I was looking for something a little bit different. I need to put quotes into the thing I’m setting. This isn’t exactly what the OP is asking about but may be related if the problem is embedded quotation marks.

I found that this, while «correct», doesn’t work because the user of this variable gets tripped by the space in the path so I needed to put quotes in there.

worked just fine so I thought I’d try the normal way of escaping quotes on Windows:

but this didn’t work, resulting in the error:

So I just opened system settings in the control panel and set the value in the environment variables GUI. Works fine now, I can type set svn at the command line and it shows quoted as I need it to be.

If you really need to set your environment variable programatically you’ll need to look at banging on the registry yourself. This is pretty straight forward in both C and Python (and probably in other languages too). It doesn’t seem as though setx can do it for you though.

Читайте также:  Пропали usb порты windows 10
Оцените статью