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.
Escape every double quote " with a caret ^ . If you want other characters with special meaning to the Windows command shell (e.g., < , > , | , & ) to be interpreted as regular characters instead, then escape them with a caret, too.
batch-file Escaping special characters Escape using caret(^) Most special characters can be escaped using the caret( ^ ).
inside double-quoted strings, use `" or "" to escape double-quotes. inside single-quoted strings, use '' to escape single-quotes.
Generally, to differentiate commands from other types of instructions, enclose the command within single or double quotation marks. When issuing TSO/E commands in an exec, it is recommended that you enclose them in double quotation marks.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With