Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 622
GetFree Avatar asked Feb 11 '09 08:02

GetFree


People also ask

How do you escape quotes in CMD?

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.

How does batch file handle special characters?

batch-file Escaping special characters Escape using caret(^) Most special characters can be escaped using the caret( ^ ).

How do you escape double quotes in DOS?

inside double-quoted strings, use `" or "" to escape double-quotes. inside single-quoted strings, use '' to escape single-quotes.

How do you use quotation marks in CMD?

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.


2 Answers

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"

like image 157
RuskoGuyachev Avatar answered Sep 29 '22 16:09

RuskoGuyachev


This is the correct way to do it:

set "myvar=c:\my music & videos" 

The quotes will not be included in the variable value.

like image 38
Craig Rorrer Avatar answered Sep 29 '22 17:09

Craig Rorrer