批量:自动转义特殊字符 [英] Batch: Auto escape special characters
问题描述
^
方法适用于几个 echo
es。 (应该是这样的:) @echo ^ |
@echo ^> ^> ^>
但是,当有很多字符要逃脱时, ^
方法不再工作了。所以,我的问题是:
有没有办法逃避所有特殊字符没有垃圾邮件的插入符?
嗯,没有必要通过运行 cmd /?
在帮助输出中排除重定向运算符和其他特殊字符在最后帮助页面的命令提示符窗口中,输出的字符串用双引号括起来。
但是使用符合 ECHO 命令也会导致双引号输出。
有几种解决方案。
第一个将字符串分配给输出到环境变量,并使用延迟扩展输出环境变量的值。
$ $ $ $ $
setlocal EnableExtensions EnableDelayedExpansion
setLine = pipe = |百分号= %%和感叹号^!
echo!Line!
setLine =重定向运算符:和>和>
echo!Line!
endlocal
或a有点短,但不太可读:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set Line = pipe = |和百分号= %%和感叹号^!& echo!Line!
setLine =重定向运算符:和>和>& echo!Line!
endlocal
注意: %
和!
必须用另一个%
和 ^
被解释为分配给环境变量的字符串中的文字字符行
。
使用子例程 PrintLine
的另一个解决方案:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
setlocal EnableExtensions DisableDelayedExpansion
call:PrintLinepipe =百分号= %%%%和感叹号!
调用:PrintLine重定向运算符:和>和>
endlocal
goto:EOF
:PrintLine
设置Line =%〜1
setlocal EnableDelayedExpansion
echo!Line!
endlocal
goto:EOF
此解决方案的缺点是:
- 百分号必须用4%的符号定义为最终打印为文字字符。
- 由于在每一行打印时使用 SETLOCAL 和 ENDLOCAL 而更慢。
阅读此答案,了解命令 SETLOCAL 和 ENDLOCAL
根据 JosefZ
的评论, a>使用 FOR 命令延迟扩展: @ echo off
setlocal EnableExtensions DisableDelayedExpansion
for %% I in (
pipe = |百分号= %%和感叹号!,
重定向运算符:<和>和>
)do echo %%〜I
endlocal
要输出的行以逗号分隔的双引号字符串列表指定,由 FOR 处理。
它具有以下优点:在延迟扩展被禁用时,必须使用百分号进行转义,但是输出的字符串不能包含双引号,除了字符串中的 。
感谢 JosefZ 的贡献。
他的答案中的 jeb 提供了其他好的解决方案。
As far as I know, I need to escape every escape characters when echoing them. The ^
method works fine for a few echo
es. (which should be something like:)
@echo ^|
@echo ^> ^>^>
However, when there are a lot of characters to escape, the ^
method won't work anymore. So, my question is:
Are there any ways escape all special characters without "spamming" the caret?
Well, there is no need to escape redirection operators and other special characters listed in last paragraph in help output by running cmd /?
in a command prompt window on last help page when the string to output is enclosed in double quotes.
But using "
on line with ECHO command results in having also the double quote output.
There are several solutions.
The first one is assigning the string to output to an environment variable and output the value of the environment variable using delayed expansion.
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Line=pipe = | and percent sign = %% and exclamation mark ^!"
echo !Line!
set "Line=redirection operators: < and > and >>"
echo !Line!
endlocal
Or a little bit shorter, but not so good readable:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "Line=pipe = | and percent sign = %% and exclamation mark ^!" & echo !Line!
set "Line=redirection operators: < and > and >>" & echo !Line!
endlocal
Note: %
and !
must be nevertheless escaped with another %
and with ^
to be interpreted as literal character in string assigned to environment variable Line
.
Another solution using a subroutine PrintLine
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
call :PrintLine "pipe = | and percent sign = %%%% and exclamation mark !"
call :PrintLine "redirection operators: < and > and >>"
endlocal
goto :EOF
:PrintLine
set "Line=%~1"
setlocal EnableDelayedExpansion
echo !Line!
endlocal
goto :EOF
The disadvantages of this solution are:
- A percent sign must be defined with 4 percent signs to be finally printed as literal character.
- It is slower because of usage of SETLOCAL and ENDLOCAL on printing each line.
Read this answer for details about the commands SETLOCAL and ENDLOCAL.
One more solution according to comment by JosefZ uses command FOR for an implicit delayed expansion:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for %%I in (
"pipe = | and percent sign = %% and exclamation mark !",
"redirection operators: < and > and >>"
) do echo %%~I
endlocal
The lines to output are specified in a comma separated list of double quoted strings for being processed by FOR.
It has the big advantage that just the percent sign must be escaped with an additional percent sign on delayed expansion being disabled. But the string to output can't contain a double quote with exception of ""
within string.
Thanks JosefZ for this contribution.
Other great solutions are provided by jeb in his answer.
这篇关于批量:自动转义特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!