我如何在批处理文件中(或从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。
+
该怎么办?
start "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8"
运行是因为PowerShell将删除引号
start
如果不加考虑就应用引号会失败。总的来说,我认为将参数括在引号中比逃避每个需要转义的字符更容易且更不容易出错。