Dealing with quotes in Windows batch scripts
In a Windows batch file, when you do the following:
set myvar="c:\my music & videos"
the variable myvar
is stored with the quotes included. Honestly I find that very stupid. The quotes are just to tell where the string begins and ends, not to be stored as part of the value itself.
How can I prevent this from happening?
Thanks.
set "myvar=c:\my music & videos"
Notice the quotes start before myvar. It's actually that simple. Side note: myvar can't be echoed afterwards unless it's wrapped in quotes because & will be read as a command separator, but it'll still work as a path.
http://ss64.com/nt/set.html under "Variable names can include Spaces"
This is the correct way to do it:
set "myvar=c:\my music & videos"
The quotes will not be included in the variable value.
It depends on how you want to use the variable. If you just want to use the value of the variable without the quotes you can use either delayed expansion and string substitution, or the for
command:
@echo OFF
SETLOCAL enabledelayedexpansion
set myvar="C:\my music & videos"
As andynormancx states, the quotes are needed since the string contains the &
. Or you can escape it with the ^
, but I think the quotes are a little cleaner.
If you use delayed expansion with string substitution, you get the value of the variable without the quotes:
@echo !myvar:"=!
>>> C:\my music & videos
You can also use the for
command:
for /f "tokens=* delims=" %%P in (%myvar%) do (
@echo %%P
)
>>> C:\my music & videos
However, if you want to use the variable in a command, you must use the quoted value or enclose the value of the variable in quotes:
-
Using string substitution and delayed expansion to use value of the variable without quotes, but use the variable in a command:
@echo OFF SETLOCAL enabledelayedexpansion set myvar="C:\my music & videos" md %myvar% @echo !myvar:"=! created.
-
Using the
for
command to use the value of the variable without quotes, but you'll have to surround the variable with quotes when using it in commands:@echo OFF set myvar="C:\my music & videos" for /f "tokens=* delims=" %%P in (%myvar%) do ( md "%%P" @echo %%P created. )
Long story short, there's really no clean way to use a path or filename that contains embedded spaces and/or &
s in a batch file.
Use jscript.
Many moons ago (i.e. about 8 years give or take) I was working on a large C++/VB6 project, and I had various bits of Batch Script to do parts of the build.
Then someone pointed me at the Joel Test, I was particularly enamoured of point 2, and set about bringing all my little build scripts into one single build script . . .
and it nearly broke my heart, getting all those little scripts working together, on different machines, with slightly different setups, ye Gods it was dreadful - particularly setting variables and parameter passing. It was really brittle, the slightest thing would break it and require 30 minutes of tweaking to get going again.
Eventually - I can be stubborn me - I chucked the whole lot in and in about a day re-wrote it all in JavaScript, running it from the command prompt with CScript.
I haven't looked back. Although these days it's MSBuild and Cruise Control, if I need to do something even slightly involved with a batch script, I use jscript.