Set a path variable with spaces in the path in a Windows .cmd file or batch file

444,667

Solution 1

Try something like this:

SET MY_PATH=C:\Folder with a space

"%MY_PATH%\MyProgram.exe" /switch1 /switch2

Solution 2

I use

set "VAR_NAME=<String With Spaces>"

when updating path:

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

Solution 3

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 :-)

Solution 4

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"
    

Solution 5

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
Share:
444,667
marcerickson
Author by

marcerickson

Updated on May 15, 2021

Comments

  • marcerickson
    marcerickson about 3 years

    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?

  • marcerickson
    marcerickson over 14 years
    I used the first method, using Replace in Notepad. The second one seemed unnecessarily complicated. Thank you.
  • Andriy M
    Andriy M almost 13 years
    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.
  • JCH2k
    JCH2k about 9 years
    I have a Batch file which gets parameters. Using set LALA=%~1was what worked for me.
  • robe007
    robe007 over 5 years
    The 2nd method works perfeclty when you are using subrutines
  • Kevin
    Kevin almost 5 years
    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.