如何在批处理文件中转义&符号? [英] How do I escape ampersands in batch files?

查看:62
本文介绍了如何在批处理文件中转义&符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

双引号不适用于 start;这开始了一个新的改为命令行窗口.

Double quotes will not work with start; this starts a new command-line window instead.

更新 1:Wael Dalloul 的解决方案有效.此外,如果有 URL 编码的字符(例如,空格被编码为%20) 在 URL 中并且它在一个批处理文件中那么 '%' 必须是编码为%%".示例中并非如此.

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.

示例,来自命令行(CMD.EXE):

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

将导致

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.

平台:Windows XP 64 位 SP2.

Platform: Windows XP 64 bit SP2.

推荐答案

来自 cmd:

  • & 像这样转义:^&(基于@Wael Dalloul 的 答案)
  • % 不需要转义
  • & is escaped like this: ^& (based on @Wael Dalloul's answer)
  • % does not need to be escaped

示例:

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 的 答案)
  • % 像这样转义:%%(基于 OP 更新)
  • & is escaped like this: ^& (based on @Wael Dalloul's answer)
  • % is escaped like this: %% (based on the OPs update)

示例:

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

这篇关于如何在批处理文件中转义&符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆