views:

842

answers:

3

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

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

vladhorby
A: 

Try something like this:

SET MY_PATH=C:\Folder with a space

"%MY_PATH\MyProgram.exe" /switch1 /switch2
aphoria
That's what I did using Replace in Notepad. Thank you.
marcerickson
Glad it helped. It would be nice if you upvote and accept whatever answer you feel helped you.
aphoria
A: 

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

Joey
I used the first method, using Replace in Notepad. The second one seemed unnecessarily complicated. Thank you.
marcerickson