处理 Windows 批处理脚本中的引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/535975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dealing with quotes in Windows batch scripts
提问by GetFree
In a Windows batch file, when you do the following:
在 Windows 批处理文件中,当您执行以下操作时:
set myvar="c:\my music & videos"
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?
变量myvar
与引号一起存储。老实说,我觉得这很愚蠢。引号只是为了说明字符串的开始和结束位置,而不是作为值本身的一部分存储。
我怎样才能防止这种情况发生?
Thanks.
谢谢。
采纳答案by RuskoGuyachev
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.
注意引号在 myvar 之前开始。其实就是这么简单。旁注:除非用引号括起来,否则 myvar 之后无法回显,因为 & 将被读取为命令分隔符,但它仍然可以作为路径使用。
http://ss64.com/nt/set.htmlunder "Variable names can include Spaces"
http://ss64.com/nt/set.html在“变量名可以包含空格”下
回答by Craig Rorrer
This is the correct way to do it:
这是正确的做法:
set "myvar=c:\my music & videos"
The quotes will not be included in the variable value.
引号将不包含在变量值中。
回答by Patrick Cuff
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:
这取决于您想如何使用变量。如果您只想使用不带引号的变量值,您可以使用延迟扩展和字符串替换,或者使用以下for
命令:
@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.
正如 andynormancx 所述,由于字符串包含&
. 或者你可以用 来逃避它^
,但我认为引号更简洁一些。
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
命令:
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:
但是,如果要在命令中使用变量,则必须使用带引号的值或将变量的值括在引号中:
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.
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. )
使用字符串替换和延迟扩展来使用不带引号的变量值,但在命令中使用变量:
@echo OFF SETLOCAL enabledelayedexpansion set myvar="C:\my music & videos" md %myvar% @echo !myvar:"=! created.
使用
for
命令使用不带引号的变量值,但在命令中使用变量时必须用引号括起来:@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.
长话短说,&
在批处理文件中使用包含嵌入空格和/或s的路径或文件名确实没有干净的方法。
回答by Binary Worrier
Use jscript.
使用 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.
许多个月前(即大约 8 年的时间),我正在从事一个大型 C++/VB6 项目,并且我有各种批处理脚本来完成部分构建。
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 . . .
然后有人指给我看Joel 测试,我特别迷恋第 2 点,并着手将我所有的小构建脚本整合到一个构建脚本中。. .
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.
它几乎让我心碎,让所有这些小脚本一起工作,在不同的机器上,设置略有不同,天哪,这太可怕了——尤其是设置变量和参数传递。它真的很脆弱,最轻微的事情都会破坏它,需要 30 分钟的调整才能重新开始。
Eventually - I can be stubborn me - I chucked the whole lot in andin about a day re-wrote it all in JavaScript, running it from the command prompt with CScript.
最终——我可能很固执——我把所有的东西都扔了进去,大约一天后用 JavaScript 重新编写了它,从命令提示符下用 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.
我没有回头。虽然现在是 MSBuild 和 Cruise Control,但如果我需要做一些与批处理脚本有关的事情,我会使用 jscript。
回答by Ben Personick
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)
Windows 命令解释器允许您在整个 set 命令周围使用引号(在从 NT 4.0 到 Windows 2012 R2 的每个 Windows NT 版本中都有效)
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.
使用 CMD 提示有时看起来很深奥,但命令解释器实际上在遵守其内部逻辑方面表现得非常可靠,您只需要重新思考。
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.
实际上,set 命令根本不需要您使用引号,但是您进行变量赋值的方式和不使用引号的 , 方法都会导致您在变量周围有额外的空格,这很难调试脚本时请注意。
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:
例如,以下两种方法都是在 Windows 命令解释器中设置变量的好方法,但双引号方法更胜一筹:
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 textHowever, 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.
但是,即使变量 IS 正确设置为该字符串,当您 ECHO 指令时,命令解释器会将 & 符号解释为关键字以指示后面的另一条语句。
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:
因此,如果您想从变量中回显字符串,CMD 解释器仍然需要被告知它是一个文本字符串,或者如果您不希望引号显示您必须执行以下操作之一:
echo the variable WITH Quotes:
用引号回显变量:
Echo."%myvar%"
echo the variable WITHOUT Quotes:
echo 变量不带引号:
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>
回答by andynormancx
You musthave 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:
例如,如果文本只是“c:\my music”,那么你可以这样做:
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.
或者正如 Dave 在他的回答中所说,您可以使用^^^转义问题字符,但要注意这种方法,因为&不是您需要转义的唯一字符。在实践中,坚持使用引号比转义所有问题字符要容易得多。
回答by Dave
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:
扩展 myvar 时要小心,因为 shell 可能不会将 & 视为文字。如果上述方法不起作用,请尝试在字符串中插入一个插入符号:
set myvar=c:\my music ^^^& videos