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

发布于 2024-08-03 09:53:07 字数 1109 浏览 7 评论 0原文

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

双引号不适用于 start;这开始了一个新的 而是使用命令行窗口。

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

例如,从命令行 (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。

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?

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

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

若水般的淡然安静女子 2024-08-10 09:53:07

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

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

违心° 2024-08-10 09:53:07

从cmd

  • &像这样转义:^&(基于@Wael Dalloul的答案
  • % 不需要转义

示例:

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 更新)

示例:

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

From a cmd:

  • & is escaped like this: ^& (based on @Wael Dalloul's answer)
  • % 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)
  • % is escaped like this: %% (based on the OPs update)

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
何以畏孤独 2024-08-10 09:53:07

如果您提供虚拟的第一个参数,则可以将其括在引号中。

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

start "" "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&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. 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"
自控 2024-08-10 09:53:07
explorer "http://www.google.com/search?client=opera&rls=...."
explorer "http://www.google.com/search?client=opera&rls=...."
恋竹姑娘 2024-08-10 09:53:07

该命令

echo this ^& that

按预期工作,输出

this & that

该命令

echo this ^& that > tmp

也有效,将字符串写入文件“tmp”。然而,在管道之前,

echo this ^& that | clip

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

echo this ^& echo that | clip

会将字符串“this”和“that”放在剪贴板上。

如果没有 ^:

echo this & echo that | clip

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

要管道 &,你必须引用它两次

echo this ^^^& that | clip

如果你把字符串放在一个变量中

set m=this ^& that

然后

set m

会​​输出

m=this & that

,但明显

echo %m%

失败,因为在 Windows 替换该变量后,导致

echo this & that

它将其解析为一个新命令并尝试执行“那个” ”。

在批处理文件中,您可以使用延迟扩展:

setlocal enableDelayedExpansion
echo !m!

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

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

在命令行上,“cmd /v”启用延迟扩展:

cmd /v /c echo !m!

即使在写入简单管道时也可以使用

cmd /v /c echo !m! | clip

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

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 ^& echo that | clip

will put the strings "this" and "that" on the clipboard.

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.

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

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:

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

On the command line, "cmd /v" enables delayed expansion:

cmd /v /c echo !m!

This works even when writing to a pipe

cmd /v /c echo !m! | clip

Simple.

秋意浓 2024-08-10 09:53:07

如果您需要echo包含&符号的字符串,引号将无济于事,因为您也会在输出中看到它们。在这种情况下,请

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

在批处理脚本中

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

使用 for: ...

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

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. In such a case, use 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
一城柳絮吹成雪 2024-08-10 09:53:07

如果文件名中有空格并且您有需要转义的字符:

您可以使用单引号和双引号来避免命令中的任何用词不当。

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

否则,^ 字符会对引号进行转义。

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.

断爱 2024-08-10 09:53:07

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

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

For special characters like '&' you can surround the entire expression with quotation marks

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文