有几个解决方案。
卡洛斯的颜色 v23c
玩弄它会有一些奇怪的输出。测试使用
call :color 04 "Test poison '&|<>!()" \n
call :color 06 ""
call :color 0A " can be printed if you use empty strings" \n
生产(德国控制台):
">" kann syntaktisch an dieser Stelle nicht verarbeitet werden.
" can be printed if you use empty strings
它有时会产生一些重大错误并且有点慢......
VT100 替代 (Win10+)
@echo off
for /f "delims=" %%E in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"') do (
set "ESC=%%E["
)
call :vtEcho 93 "No escaping needed for most characters: '&|<>!()"
call :vtEcho 94 "Problems occur with '^' and '"' though"
:vtEcho [VT100 colorcode] [message]
<NUL set /P "=%ESC%%1m%~2%ESC%0m"
echo+
exit /B 0
输出:
No escaping needed for most characters: '&|<>!()
Problems occur with '^^' and '"'
那么这是如何工作的呢?基本上使用set /P 回显几乎可以解决所有毒字符,VT 序列使用颜色代码生成颜色。我们首先使用 forfiles 生成 ESC 字符,然后在函数中使用它。但是由于我们试图将其作为单个标志提供给子例程,因此我们需要将其双引号......这意味着我们在显示它时遇到了问题。此外,set /P 只能打印单个 ^,如果您将其设为 <NUL set /P^"=^^",则在任何其他情况下都会失败。
" 和 ^ 使用标志转换的解决方法:
:vtEcho [VT100 colorcode] [message] [message] [message] ...
:: Use """" for displaying a '"'
:: Use "^" for displaying a '^'
:: Use "\n" to make a new line
<NUL set /P "=%ESC%%1m"
:__vtEcho
shift
if not "%~1" == "" (
if "%~1" == """" (
<NUL set /P ^"="""^"
goto __vtEcho
)
if "%~1" == "^^" (
<NUL set /P ^"=^^"
goto __vtEcho
)
if "%~1" == "\n" (
echo+
goto __vtEcho
)
<NUL set /P "=%~1"
goto __vtEcho
)
echo %ESC%0m
exit /B 0
测试:
call :vtEcho 96 "No problems with '" """" "' and '" "^" "' and has" \n "support for '\n'"`
结果:
No problems with '"' and '^' and has
support for '\n'
为了补偿" 和^,我们检查标志的各个部分,如果它们是错误的对应部分("" / ^^)并打印正确的输出。
但是
使用set /P 可以使您不能以空格开头。为此,没有很好的包装函数。如果你不需要,你可以使用消失的 qoutes:
@echo off
set ""="
for /f "delims=" %%E in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x1B"') do (
set "BLUE=echo !"!%%E[94m"
set "CYAN=echo !"!%%E[96m"
set "END=%%E[0m"
)
setlocal EnableDelayedExpansion
%BLUE%Has no problems with '&|<>" characters%END%
%CYAN%'^^' and "^^!" need to be escaped%END%
结果:
Has no problems with '&|<>" characters
'^' and "!" need to be escaped
我希望你能从中有所收获。感谢阅读
参考资料: