简体   繁体   English

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

[英]How do I escape ampersands in batch files?

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?如何在批处理文件(或从 Windows 命令行)中转义与号,以便使用start命令打开 URL 中带有与号的网页?

Double quotes will not work with start ;双引号不适用于start this starts a new command-line window instead.这将启动一个新的命令行窗口。

Update 1 : Wael Dalloul's solution works.更新 1 :Wael Dalloul 的解决方案有效。 In addition, if there are URL encoded characters (eg space is encoded as %20) in the URL and it is in a batch file then '%' must be encoded as '%%'.此外,如果 URL 中有 URL 编码字符(例如空格编码为 %20)并且它在批处理文件中,则 '%' 必须编码为 '%%'。 This is not the case in the example.示例中并非如此。

Example, from the command line ( CMD.EXE ):例如,从命令行( 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.平台:Windows XP 64 位 SP2。

& is used to separate commands. &用于分隔命令。 Therefore you can use ^ to escape the & .因此,您可以使用^来转义&

From a cmd :从 cmd

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

An example:一个例子:

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

From a batch file从批处理文件

  • & is escaped like this: ^& (based on @Wael Dalloul's answer ) &像这样转义: ^& (基于@Wael Dalloul 的回答
  • % is escaped like this: %% (based on the OPs update) %像这样转义: %% (基于 OP 更新)

An example:一个例子:

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

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.请注意,在这种情况下,您需要提供一个虚拟的第一个参数,因为start会将第一个参数视为新控制台窗口的标题(如果它被引用)。 So the following should work (and does here):因此,以下应该有效(并且在此处有效):

start "" "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8"
explorer "http://www.google.com/search?client=opera&rls=...."

The command命令

echo this ^& that

works as expected, outputing按预期工作,输出

this & that

The command命令

echo this ^& that > tmp

also works, writing the string to file "tmp".也可以将字符串写入文件“tmp”。 However, before a pipe然而,在一个管道之前

echo this ^& that | clip

the ^ is interpreted completely differently. ^ 的解释完全不同。 It tries to write the output of the two commands "echo this" and "that" to the pipe.它尝试将“echo this”和“that”这两个命令的输出写入管道。 The echo will work then "that" will give an error.回声会起作用,然后“那个”会出错。 Saying

echo this ^& echo that | clip

will put the strings "this" and "that" on the clipboard.将把字符串“this”和“that”放在剪贴板上。

Without the ^:没有 ^:

echo this & echo that | clip

the first echo will write to the console and only the second echo's output will be piped to clip (similarly for "> tmp" redirection).第一个回声将写入控制台,只有第二个回声的输出将通过管道传输到剪辑(类似于“> tmp”重定向)。 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要管道一个 &,你必须引用它两次

echo this ^^^& that | clip

If you put the string in a variable如果你把字符串放在一个变量中

set m=this ^& that

then然后

set m

will output会输出

m=this & that

but the obvious但很明显

echo %m%

fails because, after Windows substitutes the variable, resulting in失败,因为在 Windows 替换变量后,导致

echo this & that

it parses this as a new command and tries to execute "that".它将它解析为一个新命令并尝试执行“那个”。

In a batch file, you can use delayed expansion :在批处理文件中,您可以使用延迟扩展

setlocal enableDelayedExpansion
echo !m!

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:要输出到管道,我们必须用 ^& 替换变量值中的所有 &,我们可以使用 %VAR:FROM=TO% 语法来做到这一点:

echo !m:^&=^^^&! | clip

On the command line, "cmd /v" enables delayed expansion:在命令行中,“cmd /v”启用延迟扩展:

cmd /v /c echo !m!

This works even when writing to a pipe即使在写入管道时这也有效

cmd /v /c echo !m! | clip

Simple.简单的。

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.如果您需要echo包含与号的字符串,引号将无济于事,因为您也会在输出中看到它们。 In such a case, use for :在这种情况下,请使用for

for %a in ("First & Last") do echo %~a

...in a batch script: ...在批处理脚本中:

for %%a in ("First & Last") do echo %%~a

or或者

for %%a in ("%~1") do echo %%~a

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.您可以使用单引号和双引号来避免命令中的任何用词不当。

scp ./'files name with spaces/internal folder with spaces/"text & files stored.txt"' .

The ^ character escapes the quotes otherwise.否则^字符会转义引号。

对于像 '&' 这样的特殊字符,您可以用引号将整个表达式括起来

set "url=https://url?retry=true&w=majority"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM