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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1327431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:40:58  来源:igfitidea点击:

How do I escape ampersands in batch files?

windowsbatch-fileescapingampersand

提问by Peter Mortensen

How do I escape ampersands in a batch file (or from the Windows command line) in order to use the startcommand to open web pages with ampersands in the URL?

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

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

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

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 filethen '%' must be encoded as '%%'. This is not the case in the example.

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

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。

采纳答案by Lasse Christiansen

From a cmd:

从 cmd

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

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

回答by Wael Dalloul

&is used to separate commands. Therefore you can use ^to escape the &.

&用于分隔命令。因此,您可以使用^转义&.

回答by belugabob

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 startwill treat the first argument as a title for the new console windows, if it is quoted. So the following should work (and does here):

请注意,在这种情况下,您需要提供一个虚拟的第一个参数,如果start第一个参数被引用,则会将第一个参数视为新控制台窗口的标题。因此,以下应该有效(并且在此处有效):

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

回答by adatapost

explorer "http://www.google.com/search?client=opera&rls=...."

回答by Denis Howe

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". However, before a pipe

也可以将字符串写入文件“tmp”。然而,在一个管道之前

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. The echo will work then "that" will give an error. Saying

^ 的解释完全不同。它尝试将“echo this”和“that”这两个命令的输出写入管道。回声会起作用,然后“那个”会出错。说

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). So, when output is being redirected, the ^ does not quote the & but instead causes it to be applied before the redirection rather than after.

第一个回声将写入控制台,只有第二个回声的输出将通过管道传输到剪辑(类似于“> tmp”重定向)。因此,当输出被重定向时, ^ 不会引用 & 而是使其在重定向之前而不是之后应用。

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.

简单的。

回答by Jaroslav Záruba

If you need to echoa string that contains an ampersand, quotes won't help, because you would see them on the output as well. In such a case, use for:

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

回答by be-ns

Ifyou have spaces in the name of the file andyou 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.

^字符转义,否则引号。