Dealing with quotes in Windows batch scripts

WindowsBatch FileQuotesScripting

Windows Problem Overview


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.

Windows Solutions


Solution 1 - Windows

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"

Solution 2 - Windows

This is the correct way to do it:

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

The quotes will not be included in the variable value.

Solution 3 - Windows

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:

  1. 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.
    
  2. 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.

Solution 4 - Windows

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.

Solution 5 - Windows

The Windows command interpreter allows you to use the quotes around the entire set command (valid in every version of windows NT from NT 4.0 to Windows 2012 R2)

Your script should just be written as follows:

@echo OFF

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

Then you may put quotes around the variables as needed.

Working with the CMD prompt can seem esoteric at times, but the command interpreter actually behaves pretty solidly in obeying it's internal logic, you just need to re-think things.

In fact, the set command does not require you to use quotes at all, but both the way you are doing your variable assignment and the way the ,method of using no quotes can cause you to have extra spaces around your variable which are hard to notice when debugging your script.

e.g. Both of the below are technically Valid, but you can have trailing spaces, so it's not a good practice:

set myvar=some text   
set myvar="some text" 

e.g. Both of the below are good methods for setting variables in Windows Command interpreter, however the double quote method is superior:

set "myvar=Some text"
(set myvar=Some value)

Both of these leave nothing to interpretation the variable will have exactly the data you are looking for.

strong text However, for your purposes, only the quoted method will work validly because you are using a reserved character

Thus, you would use:

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

However, even though the variable IS correctly set to this string, when you ECHO the sting the command interpreter will interpret the ampersand as the keyword to indicate another statement follows.

SO if you want to echo the string from the variable the CMD interpreter still needs to be told it's a text string, or if you do not want the quotes to show you have to do one of the following:

echo the variable WITH Quotes:

Echo."%myvar%"

echo the variable WITHOUT Quotes:

Echo.%myvar:&=^&%
<nul SET /P="%myvar%"

In the above two scenarios you can echo the string with no quotes just fine. Example output below:

C:\Admin>    Echo.%myvar:&=^&%
C:\my music & videos

C:\Admin>    <nul SET /P="%myvar%"
C:\my music & videos
C:\Admin>

Solution 6 - Windows

Try using the escape character '^', e.g.

set myvar=c:\my music ^& videos

You'll have you be careful when you expand myvar because the shell might not treat the & as a literal. If the above doesn't work, try inserting a caret into the string too:

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

Solution 7 - Windows

Two solutions:

  1. Don't use spaces or other characters that are special to the command interpreter in path names (directory or file names). If you use only letters, numbers, underscores, and hyphens (and a period before the extension to identify the file type), your scripting life will become immeasurably simpler.

    I have written and otherwise collected a plethora of tools over the years, including a DOS utility that will rename files. (It began as something that just removed spaces from filenames, but morphed into something that will replace characters or strings within the filenames, even recursively.) If anyone's interested, I will get my long-neglected web site up and running and post this and others.

    That said, variables aren't just for holding pathnames, so...

  2. As others have already pointed out, SET "myvar=c:\my music & videos" is the correct workaround for such a variable value. (Yes, I said workaround. I agree that your initial inclination to just quote the value ("my music & videos") is far more intuitive, but it is what it is, as they say.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGetFreeView Question on Stackoverflow
Solution 1 - WindowsRuskoGuyachevView Answer on Stackoverflow
Solution 2 - WindowsCraig RorrerView Answer on Stackoverflow
Solution 3 - WindowsPatrick CuffView Answer on Stackoverflow
Solution 4 - WindowsBinary WorrierView Answer on Stackoverflow
Solution 5 - WindowsBen PersonickView Answer on Stackoverflow
Solution 6 - WindowsDaveView Answer on Stackoverflow
Solution 7 - WindowsBobView Answer on Stackoverflow