2

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 quotes -- any way to escape those too?

user541686
  • 205,094
  • 128
  • 528
  • 886
  • +1: That's a good question ain't it? =D I tried playing around with a mix of ^, & and | around the variable with different quotes, back quotes etc. (even though some of the variations are silly and I knew it wouldn't work), but can't seem to escape the quotes. – Anthony Miller Aug 23 '11 at 21:34

3 Answers3

0

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\

jeb
  • 78,592
  • 17
  • 171
  • 225
  • Wait, `reg` **unescapes** the strings I give it? I didn't see this documented anywhere -- are you sure? – user541686 Aug 23 '11 at 17:46
  • Nearly, I tested it and it works with `"C:\Hello""world\\"`, but it seems to be a bit more complex, see my edit – jeb Aug 23 '11 at 23:03
  • My guess is that it follows [these rules](http://msdn.microsoft.com/en-us/library/bb776391.aspx), though I could be wrong. I don't believe it has anything to do with Reg. – user541686 Aug 24 '11 at 00:22
  • But even if `reg.exe` uses the `CommandLineToArgvW` function, it's `reg` who do the unescape, and it doesn't explain why "" results into a single quote – jeb Aug 24 '11 at 22:40
  • No. `cmd.exe` doesn't use this sort of escaping. A simple test shows the result `echo "One""two\\"` results in the output of `"One""two\\"` – jeb Aug 24 '11 at 23:03
  • Echo is a special command -- it's the worst thing you could possibly test with! (That doesn't mean you're wrong, though.) – user541686 Aug 24 '11 at 23:12
  • But even `ECHO` doesn't affect the expansion of the rest, for an explanation of the expansion rules try a look at [How does the Windows Command Interpreter (CMD.EXE) parse scripts?](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133) – jeb Aug 25 '11 at 20:19
-1

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

Rakesh
  • 4,264
  • 4
  • 32
  • 58
-1

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.

Ben
  • 51,770
  • 36
  • 127
  • 149