Supress%(百分号)在CMD文件中转义? (终极版)

时间:2022-06-05 06:29:05

After researching, I think I know the answer to this: no. But just in case I am missing something: Is there a way to globally suppress % escaping in a CMD file? Something like:

经过研究,我想我知道答案:不。但是,万一我错过了一些东西:有没有办法在CMD文件中全局抑制%转义?就像是:

SETLOCAL NOESCAPE
MYAPP.EXE "%41%42%43%44"

So MYAPP.EXE sees the literal string:

所以MYAPP.EXE看到文字字符串:

"%41%42%43%44"

I know I can double up on the percent signs but was wondering if there is a setting that can be switched on and off.

我知道我可以加倍百分号,但想知道是否有可以打开和关闭的设置。

Thanks.

1 个解决方案

#1


No, there is no setting that allows an executed batch line to treat a percent as a literal. Percent interpretation is an integral part of how the batch parser works - See https://*.com/a/4095133/1012053 for more info.

不,没有允许执行的批处理行将百分比视为文字的设置。百分比解释是批处理解析器工作方式的一个组成部分 - 有关详细信息,请参阅https://*.com/a/4095133/1012053。

If you want a percent literal within an executed batch script line, than it must be doubled.

如果要在执行的批处理脚本行中使用百分比文字,则必须加倍。

I have thought of a method of embedding and executing a command containing un-escaped percent literals, but it is an ugly hack. You put the string literal in a "labeled" comment, use FINDSTR to extract the string literal, and process that value via FOR /F. You can use multiple labels to selectively get the string you need.

我想到了一种嵌入和执行包含未转义百分比文字的命令的方法,但这是一个丑陋的黑客。您将字符串文字放在“标记”注释中,使用FINDSTR提取字符串文字,并通过FOR / F处理该值。您可以使用多个标签来有选择地获取所需的字符串。

@echo off
::cmd1:myapp.exe "%41%42%43%44"
::cmd2:myapp.exe "%91%92%93%94"
for /f "tokens=1* delims=:" %%A in ('findstr "^::cmd1:" "%~f0"') do %%B
for /f "tokens=1* delims=:" %%A in ('findstr "^::cmd2:" "%~f0"') do %%B

I don't know about you, but I would rather simply escape the percents ;-)

我不了解你,但我宁愿逃避百分比;-)

#1


No, there is no setting that allows an executed batch line to treat a percent as a literal. Percent interpretation is an integral part of how the batch parser works - See https://*.com/a/4095133/1012053 for more info.

不,没有允许执行的批处理行将百分比视为文字的设置。百分比解释是批处理解析器工作方式的一个组成部分 - 有关详细信息,请参阅https://*.com/a/4095133/1012053。

If you want a percent literal within an executed batch script line, than it must be doubled.

如果要在执行的批处理脚本行中使用百分比文字,则必须加倍。

I have thought of a method of embedding and executing a command containing un-escaped percent literals, but it is an ugly hack. You put the string literal in a "labeled" comment, use FINDSTR to extract the string literal, and process that value via FOR /F. You can use multiple labels to selectively get the string you need.

我想到了一种嵌入和执行包含未转义百分比文字的命令的方法,但这是一个丑陋的黑客。您将字符串文字放在“标记”注释中,使用FINDSTR提取字符串文字,并通过FOR / F处理该值。您可以使用多个标签来有选择地获取所需的字符串。

@echo off
::cmd1:myapp.exe "%41%42%43%44"
::cmd2:myapp.exe "%91%92%93%94"
for /f "tokens=1* delims=:" %%A in ('findstr "^::cmd1:" "%~f0"') do %%B
for /f "tokens=1* delims=:" %%A in ('findstr "^::cmd2:" "%~f0"') do %%B

I don't know about you, but I would rather simply escape the percents ;-)

我不了解你,但我宁愿逃避百分比;-)