如何在批处理文件中转义&符号?
如何在批处理文件中(或从 Windows 命令行),以便使用 start
命令 打开 URL 中带有 & 符号的网页?
双引号不适用于 start
;这开始了一个新的 而是使用命令行窗口。
更新 1:Wael Dalloul 的解决方案有效。另外,如果 有 URL 编码字符(例如空格被编码为 %20) 在 URL 中并且它在批处理文件中,那么“%”必须是 编码为“%%”。示例中的情况并非如此。
例如,从命令行 (CMD.EXE
):
start http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8
将导致
http://www.google.com/search?client=opera
在默认浏览器中打开并在命令行窗口中出现这些错误:
'rls' is not recognized as an internal or external command,
operable program or batch file.
'q' is not recognized as an internal or external command,
operable program or batch file.
'sourceid' is not recognized as an internal or external command,
operable program or batch file.
'ie' is not recognized as an internal or external command,
operable program or batch file.
'oe' is not recognized as an internal or external command,
operable program or batch file.
平台:Windows XP 64 位 SP2。
How do I escape ampersands in a batch file (or from the
Windows command line) in order to use the start
command to
open web pages with ampersands in the URL?
Double quotes will not work with start
; this starts a new
command-line window instead.
Update 1: Wael Dalloul's solution works. In addition, if
there are URL encoded characters (e.g. space is encoded as
%20) in the URL and it is in a batch file then '%' must be
encoded as '%%'. This is not the case in the example.
Example, from the command line (CMD.EXE
):
start http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8
will result in
http://www.google.com/search?client=opera
being opened in the default browser and these errors in the command line window:
'rls' is not recognized as an internal or external command,
operable program or batch file.
'q' is not recognized as an internal or external command,
operable program or batch file.
'sourceid' is not recognized as an internal or external command,
operable program or batch file.
'ie' is not recognized as an internal or external command,
operable program or batch file.
'oe' is not recognized as an internal or external command,
operable program or batch file.
Platform: Windows XP 64 bit SP2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
&
用于分隔命令。因此,您可以使用^
来转义&
。&
is used to separate commands. Therefore you can use^
to escape the&
.从cmd:
&
像这样转义:^&
(基于@Wael Dalloul的答案)%
不需要转义示例:
来自批处理文件
&
像这样转义:^&
(基于 @Wael Dalloul 的答案 )%
的转义如下:%%
(基于 OP 更新)示例:
From a cmd:
&
is escaped like this:^&
(based on @Wael Dalloul's answer)%
does not need to be escapedAn example:
From a batch file
&
is escaped like this:^&
(based on @Wael Dalloul's answer)%
is escaped like this:%%
(based on the OPs update)An example:
如果您提供虚拟的第一个参数,则可以将其括在引号中。
请注意,在这种情况下,您需要提供一个虚拟的第一个参数,因为
start
会将第一个参数视为新控制台窗口的标题(如果它被引用)。因此,以下内容应该有效(并且在这里有效):You can enclose it in quotes, if you supply a dummy first argument.
Note that you need to supply a dummy first argument in this case, as
start
will treat the first argument as a title for the new console windows, if it is quoted. So the following should work (and does here):该命令
按预期工作,输出
该命令
也有效,将字符串写入文件“tmp”。然而,在管道之前,
^ 的解释完全不同。它尝试将两个命令“echo this”和“that”的输出写入管道。回声将起作用,然后“that”将给出错误。 Saying
会将字符串“this”和“that”放在剪贴板上。
如果没有 ^:
第一个 echo 将写入控制台,只有第二个 echo 的输出将通过管道传输到剪辑(与“> tmp”重定向类似)。因此,当输出被重定向时,^ 不会引用 &而是导致它在重定向之前而不是之后应用。
要管道 &,你必须引用它两次
如果你把字符串放在一个变量中
然后
会输出
,但明显
失败,因为在 Windows 替换该变量后,导致
它将其解析为一个新命令并尝试执行“那个” ”。
在批处理文件中,您可以使用延迟扩展:
要输出到管道,我们必须用 ^& 替换变量值中的所有 &,我们可以使用 %VAR 来做到这一点: FROM=TO% 语法:
在命令行上,“cmd /v”启用延迟扩展:
即使在写入简单管道时也可以使用
。
The command
works as expected, outputing
The command
also works, writing the string to file "tmp". However, before a pipe
the ^ is interpreted completely differently. It tries to write the output of the two commands "echo this" and "that" to the pipe. The echo will work then "that" will give an error. Saying
will put the strings "this" and "that" on the clipboard.
Without the ^:
the first echo will write to the console and only the second echo's output will be piped to clip (similarly for "> tmp" redirection). So, when output is being redirected, the ^ does not quote the & but instead causes it to be applied before the redirection rather than after.
To pipe an &, you have to quote it twice
If you put the string in a variable
then
will output
but the obvious
fails because, after Windows substitutes the variable, resulting in
it parses this as a new command and tries to execute "that".
In a batch file, you can use delayed expansion:
To output to a pipe, we have to replace all &s in the variable value with ^&, which we can do with the %VAR:FROM=TO% syntax:
On the command line, "cmd /v" enables delayed expansion:
This works even when writing to a pipe
Simple.
如果您需要
echo
包含&符号的字符串,引号将无济于事,因为您也会在输出中看到它们。在这种情况下,请在批处理脚本中
使用
for
: ...If you need to
echo
a string that contains an ampersand, quotes won't help, because you would see them on the output as well. In such a case, usefor
:...in a batch script:
or
如果文件名中有空格并且您有需要转义的字符:
您可以使用单引号和双引号来避免命令中的任何用词不当。
否则,
^
字符会对引号进行转义。If you have spaces in the name of the file and you have a character you need to escape:
You can use single AND double quotes to avoid any misnomers in the command.
The
^
character escapes the quotes otherwise.对于像“&”这样的特殊字符您可以用引号将整个表达式括起来
set "url=https://url?retry=true&w=majority"
For special characters like '&' you can surround the entire expression with quotation marks
set "url=https://url?retry=true&w=majority"