在下面的 .BAT 文件中,测试 2 演示了括号内的代码块中的颜色错误,测试 3 演示了 FOR 循环中的错误,测试 4 显示了如何通过调用不执行任何操作的子程序来缓解该错误(调用:resetANSI)我的问题是:
这个错误的本质是什么......为什么在代码块括号内,将管道传输到 FINDSTR 后,内联颜色代码是否会失败?这个错误是 FINDSTR 特有的,还是更普遍的?(FINDSTR 有一些已知错误,但我没有看到这个错误列在其中。)
调用不执行任何操作的子程序是缓解此错误的最佳方法吗?
代码下面是显示输出的屏幕截图,表明在测试 2 和 3 中应该以洋红色显示的行中的颜色代码失败。
在此先向所有试图提供帮助的人表示感谢!
[编辑于 2020 年 3 月 26 日:由于论坛未在 .bat 代码中的颜色代码定义中显示 Esc 字符,因此我编辑了 .bat 代码,以便它在运行时生成 Esc 字符。]
@echo off
goto :main
:resetANSI
EXIT /B
:main
setlocal EnableDelayedExpansion
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
set "green=%ESCchar%[92m"
set "yellow=%ESCchar%[93m"
set "magenta=%ESCchar%[95m"
set "cyan=%ESCchar%[96m"
set "white=%ESCchar%[97m"
echo %white%Test 1 is NOT in a FOR loop nor within parentheses.
echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta, and FINDSTR found and displayed 'success'.%yellow%
echo %cyan%Test 1 completed.
echo %white%Test 2 is within parentheses.
( echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta, and FINDSTR found and displayed 'success'.%yellow%
)
echo %cyan%Test 2 completed.
echo %white%Test 3 is within a FOR loop.
for /L %%G in (3,1,3) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
echo %magenta%This is supposed to be magenta, and FINDSTR found and displayed 'success'.%yellow%
)
echo %cyan%Test 3 completed.%white%
echo %white%Test 4 is within a FOR loop and includes a call/return after the pipe to FINDSTR.
for /L %%G in (4,1,4) do (
echo %yellow%[Test %%G] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo %Next, the string 'success' will be piped to FINDSTR...
echo success | findstr /R success
call :resetANSI
echo %magenta%This is supposed to be magenta, and FINDSTR found and displayed 'success'.%yellow%
)
echo %cyan%Test 4 completed.%white%
exit /B
答案1
在用户 vssher 尝试回答该问题之后(vssher 在发现无法正常工作后删除了他的尝试),我认为我找到了最佳解决方案:将 FINDSTR 命令放在嵌套括号内。该解决方案在以下 .bat 代码的测试 3 和 4 中进行了演示,显示输出的屏幕截图显示在 .bat 代码下方:
@echo off
setlocal EnableDelayedExpansion
rem Define some useful colorcode vars:
for /F "delims=#" %%E in ('"prompt #$E# & for %%E in (1) do rem"') do set "ESCchar=%%E"
set "green=%ESCchar%[92m"
set "yellow=%ESCchar%[93m"
set "magenta=%ESCchar%[95m"
set "cyan=%ESCchar%[96m"
set "white=%ESCchar%[97m"
set "black=%ESCchar%[30m"
echo %white%Test 1 is NOT in a FOR loop nor within parentheses, and color works right.
echo %yellow%[Test 1] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo Next, the string 'success' will be piped to FINDSTR...%white%
echo success | findstr /R success
echo %magenta%This is magenta and FINDSTR found and displayed 'success' in white.%yellow%
echo %green%This is green.
echo %cyan%Test 1 completed.
echo:
echo %white%Test 2 is within parentheses, and color stops working after the pipe to FINDSTR.
( echo %yellow%[Test 2] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo Next, the string 'success' will be piped to FINDSTR...%white%
echo success | findstr /R success
echo %magenta%This is supposed to be magenta and FINDSTR found and displayed 'success' in white.
echo %green%This is supposed to be green.
)
echo %cyan%Test 2 completed.
echo:
echo %white%Test 3 is within parentheses, but color works because FINDSTR is nested within parentheses.
( echo %yellow%[Test 3] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo Next, the string 'success' will be piped to FINDSTR...%white%
echo success | ( findstr /R success )
echo %magenta%This is magenta and FINDSTR found and displayed 'success' in white.
echo %green%This is green.
)
echo %cyan%Test 3 completed.
echo:
echo %white%Test 4 is within parentheses, FINDSTR is nested within parentheses,
echo and FINDSTR is piped a string that does NOT match what it's searching for.
( echo %yellow%[Test 4] %green%This is Green, %magenta%this is Magenta, and %yellow%this is Yellow.
echo Next, the string 'failed' will be piped to FINDSTR...%white%
echo failed | ( findstr /R success )
echo %magenta%This is magenta and FINDSTR correctly displayed nothing.
echo %green%This is green.
)
echo %cyan%Test 4 completed.