143

I'm new to script writing and can't get this one to work. I could if I moved the files to a path without a space in it, but I'd like it to work with the space if it could.

I want to extract a bunch of Office updates to a folder with a .cmd file. To make the batch file usable on any computer, I set a path variable which I only have to change in one place to run it on another machine. The problem is that the path has a space in it. If I put quotes around the path in the definition, cmd.exe puts them around the path before it appends the filename and switches and the batch fails with "Command line syntax error." Without quotes, it fails with, "is not recognized as an internal or external command, operable program, or batch file."

For testing, I'm using the help switch until or if I can get it working. I can do it using an 8.3 file/folder name (e.g. My Documents as MyDocu~1), but can it be done a different way?

1
  • The quotes must contain the path with the file name and the command line parameters must follow. Can you give some more details about how the command line gets created? Exactly waht do you mean by > If I put quotes around the path in the definition, cmd.exe puts them around the path before it appends the filename and switches Commented Dec 5, 2009 at 3:16

11 Answers 11

118

Try something like this:

SET MY_PATH=C:\Folder with a space

"%MY_PATH%\MyProgram.exe" /switch1 /switch2
Sign up to request clarification or add additional context in comments.

2 Comments

This answer would be better if the switches were explained
@AFOC There is nothing to explain about the switches…they are not real. It is just an example of calling a program with spaces in the path.
117

I use

set "VAR_NAME=<String With Spaces>"

when updating path:

set "PATH=%UTIL_DIR%;%PATH%"

3 Comments

this should be the accepted answer. for some reason, calling set from an if block doesn't work if the value has a space and is unquoted.
@Kevin I think it's not caused by spaces, simple set A=1 2 3 is ok in if block, but something like set B=(x86)\Nvidia is not ok inside if block, but if you move it out of if block then it can run without error again. I think it's somehow recognized as a command rather than a path.
To clarify (for simpletons like me), UTIL_DIR in the second line should really be VAR_NAME
36

There are two options here. First, you can store the path unquoted and just quote it later:

set MyPath=C:\Program Files\Foo
"%MyPath%\foo with spaces.exe" something

Another option you could use is a subroutine which alles for un-quoting strings (but in this case it's actually not a very good idea since you're adding quotes, stripping them away and re-adding them again without benefit):

set MyPath="C:\Program Files\Foo"
call :foo %MyPath%
goto :eof

:foo
"%~1\foo.exe"
goto :eof

The %~1 removes quotation marks around the argument. This comes in handy when passing folder names around quoted but, as said before, in this particular case it's not the best idea :-)

3 Comments

I used the first method, using Replace in Notepad. The second one seemed unnecessarily complicated. Thank you.
I have a Batch file which gets parameters. Using set LALA=%~1was what worked for me.
The 2nd method works perfeclty when you are using subrutines
17

Try this;

  1. create a variable as below

    SET "SolutionDir=C:\Test projects\Automation tests\bin\Debug"**
    
  2. Then replace the path with variable. Make sure to add quotes for starts and end

    vstest.console.exe "%SolutionDir%\Automation.Specs.dll"
    

1 Comment

Worked great. The main thing is the unconvetional syntax of having the variable assignment inside the quotes. What do the double asterisks (**) at the end do?
8

I always place the path in double quotes when I am creating a .bat file. (I just added the PAUSE so it wont close the screen.)

For example:

"C:\Program Files\PageTech\PCLReader64_131\PCLReader64.exe"
PAUSE

Comments

8

The proper way to do this is like so:

@ECHO off
SET MY_PATH=M:\Dir\^
With Spaces\Sub Folder^
\Dir\Folder
:: calls M:\Dir\With Spaces\Sub Folder\Dir\Folder\hello.bat
CALL "%MY_PATH%\hello.bat"
pause

1 Comment

"The ^ symbol (also called caret or circumflex) is an escape character in Batch script. When it is used, the next character is interpreted as an ordinary character. ..." - stackoverflow.com/a/20342902/1523779
7

I had this same problem recently, imagine...

C:\ (root)
└  Folder with spaces
     └  File1.txt
     └  File2.txt

❌ Issue [What NOT to do]

You're right, if you add " " around the path...

SET RootFolder="C:\Folder with spaces"

it then makes it unusable if you want to then append filenames, etc (without doing some interim processing on the string...

FOR %%F IN * DO (COPY %%F "%RootFolder%\%%F")

COPY File1.txt ""C:\Folder with spaces"\File1.txt"  <-- ❌ Invalid path
COPY File2.txt ""C:\Folder with spaces"\File2.txt"  <-- ❌ Invalid path

✅ Solution [What to do]

The key is to still use " ", but put them around the entire SET statement (i.e. BEFORE the variable name)

SET "RootFolder=C:\Folder with spaces"

This will them work when you need to re-use the variable

FOR %%F IN * DO (COPY %%F "%RootFolder%\%%F")

COPY File1.txt "C:\Folder with spaces\File1.txt"
COPY File1.txt "C:\Folder with spaces\File2.txt"

Comments

5

The easiest way to fix this problem is to put the folder name in quotes:

(cd\New Folder\...) --> (cd\"New Folder"\...)

Hopes this helps.

Comments

2

If you need to store permanent path (path is not changed when cmd is restart)

  1. Run the Command Prompt as administrator (Right click on cmd.exe and select run as administrator)

  2. In cmd setx path "%path%;your new path" then enter

  3. Check whether the path is taken correctly by typing path and pressing enter

1 Comment

I think, the question is not about the PATH variable, but rather about a path variable. The OP apparently wants to store a particular path into a variable and use the value across the batch script.
2

also just try adding double slashes like this works for me only

set dir="C:\\\1. Some Folder\\\Some Other Folder\\\Just Because"

@echo on
MKDIR %dir%

OMG after posting they removed the second \ in my post so if you open my comment and it shows three you should read them as two......

Comments

0

If you don't want/need to declare a variable in your batch script, I've worked around this issue for a path with spaces in a script call by adding double quotes to the whole path and single quotes to he folder with the spaces:

powershell.exe "C:\FolderWithoutSpaces\'Folder With Spaces'\SubFolderWithoutSpaces\ScriptFile.ps1"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.