Dealing with quotes in Windows batch scripts

Deadly 提交于 2019-11-26 10:25:25

问题


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.


回答1:


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"




回答2:


This is the correct way to do it:

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

The quotes will not be included in the variable value.




回答3:


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.




回答4:


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.




回答5:


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>



回答6:


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



回答7:


You must have the quotes if the text you are setting includes certain characters, including &. If your text didn't include & then you wouldn't need the quotes.

For example if the text was just "c:\my music" then you could do:

set myvar = c:\my music

But because your text has a & you need the quotes.

Edit:

Or as Dave says in his answer you can escape the problem characters with ^^^, but beware of that approach as & isn't the only character you need to escape. In practice it is far easier to stick with the quotes rather than escaping all the problem characters.



来源:https://stackoverflow.com/questions/535975/dealing-with-quotes-in-windows-batch-scripts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!