一尘不染

如何在批处理文件中转义 & 符号?

all

如何在批处理文件(或从 Windows 命令行)中转义 & 符号,以便使用该start命令打开 URL 中带有 & 符号的网页?

双引号不适用于start; 这将启动一个新的命令行窗口。

更新 1 :Wael Dalloul 的解决方案有效。此外,如果 URL 中有 URL 编码字符(例如空格编码为 %20)并且
它在批处理文件中, 则 ‘%’ 必须编码为 ‘%%’。示例中不是这种情况。

例如,从命令行 ( 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。


阅读 52

收藏
2022-09-04

共1个答案

一尘不染

从 cmd

  • &像这样逃脱:(^&基于@Wael Dalloul的回答
  • %不需要逃避

一个例子:

start http://www.google.com/search?client=opera^&rls=en^&q=escape+ampersand%20and%20percentage+in+cmd^&sourceid=opera^&ie=utf-8^&oe=utf-8

从批处理文件

  • &像这样逃脱:(^&基于@Wael Dalloul的回答
  • %像这样转义:(%%基于 OPs 更新)

一个例子:

start http://www.google.com/search?client=opera^&rls=en^&q=escape+ampersand%%20and%%20percentage+in+batch+file^&sourceid=opera^&ie=utf-8^&oe=utf-8
2022-09-04