5

In the following .BAT file, test 2 demonstrates the color bug inside a parenthesized block of code, test 3 demonstrates the bug inside a FOR loop, and test 4 shows how the bug can be mitigated by calling a do-nothing subroutine (call :resetANSI). My questions are:

  1. What is the nature of the bug... why do inline color codes fail after the piping to FINDSTR within a parenthesized block of code? Is this bug specific to FINDSTR, or more general? (FINDSTR has some known bugs, but I didn't see this one listed among them.)

  2. Is calling the do-nothing subroutine the best way to mitigate this bug?

Below the code is a screencapture of the display output, that shows the color code fails in tests 2 & 3 in the lines that are supposed to display in magenta.

Thanks in advance to whoever tries to help!

[EDIT 3/26/2020: Because the forum didn't display the Esc character in the colorcode definitions in the .bat code, I edited the .bat code so it will generate the Esc character at runtime.]

@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

Screen capture of output of .BAT program

2 Answers2

3

Building on an attempt by user vssher to answer the question (vssher deleted his/her attempt after it was found not to work right) I think I've found the best solution: place the FINDSTR command within nested parentheses. The solution is demonstrated in tests 3 and 4 of the following .bat code, and a screencapture of the display output is shown below the .bat code:

@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.

Display output of the .bat code

0

Update: The behavior in the OP appears to have been fixed in Windows 11, but still occurs on Windows 10.

A bit of a necro, but I ran into this same problem today working on a batch script, but the fix above actually didn't work for me because it caused another problem with setting the error level when using Dir results piped through to find/findstr for Case-Sensitive validation.

For some reason when I tried something along the lines of the following:

setlocal enabledelayedexpansion
cd Subfolder
set filename= myfile.txt
dir /B *.txt | (find "!filename!")
echo !ERRORLEVEL!
if !ERRORLEVEL! EQU 1 (
   echo Match failed
) else (
   echo Match Succeeded
)

The errorlevel would always return 1.

Interestingly, after a bit of trial and error, I found if I took the parentheses off of the find command, and then used >nul after it (since I didn't want the output there anyways), it actually fixed the problem with the color codes not working and the error level reporting as well:

setlocal enabledelayedexpansion
cd Subfolder
set filename= myfile.txt
dir /B *.txt | find "!filename!" >nul
echo !ERRORLEVEL!
if !ERRORLEVEL! EQU 1 (
   echo Match failed
) else (
   echo Match Succeeded
)

I implemented the same fix to the original post code and the fix does work, however the default find/findstr output gets removed with >nul or 1>nul, which may not be viable for you depending on your situation. (Notice in the output image the "success" output from the findstr command is omitted)

Examples:

  • Appending >nul

Code:

@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 >nul 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 >nul 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 >nul 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 >nul 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

Output: >nul output