What is the proper way to quote a variable in a batch file?
I've found that something like "%~dp0"
doesn't work because it turns out to be something like "C:\Windows\"
, which in turn gets interpreted as having an escaped quote at the end.
Oh and of course there's always problems with embedded quo开发者_StackOverflowtes -- any way to escape those too?
You are mixing a bit the different quote/escape problems here.
In batch itself there are different rules for quotes and escapes than for reg
.
In batch a caret escapes the next character, but only outside of quotes (there is only one exception for escaping an exclamation mark inside of quotes).
reg.exe
uses the \
to escape the next character, EDIT: but it seems that it only escapes a quote character.
To embedd a single quote it's easier to use two quotes.
Only a backslash at the end of the content is a problem, as it escapes the last quote.
Reg Add HKCU\Temp /d "One""two\\" /t REG_SZ
Adds One"two\
Which variable are you trying to quote ? If you have defined the variables in the environment/system varaibles, then you can quote it as %variable_name%
.
For example if you need to quote the 'path' variable, then %path%
is the right way
If you want to pass something with spaces into a variable use ` ( top left of the keyboard ), this should also deal with embedded quotes. If you're passing a variable with spaces into another batch use double quotes.
精彩评论