如何在批处理文件(或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
中的一个怪癖,如果不加思考地应用,引用参数会失败。总的来说,我认为将参数括在引号中比在其中需要转义每个需要转义的字符更容易且更少出错。 - Joey+
前面应该放什么来转义它? - williamstart "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8"
可以正常工作,因为PowerShell会去掉引号。 - phuclv