It looks like it does not really matter how often the searched string exists in file. It looks like you want to know only if the file contains the string or not. In this case it is enough to evaluate the exit code of find.exe
or findstr.exe
which can be both used to search for a string in a file. Both exit with 1
if there is no match for searched string and with 0
on at least one occurrence found.
@echo off
%SystemRoot%\System32\findstr.exe /L /I /M /C:FAIL "C:\output\summary.txt" >nul
if errorlevel 1 exit /B 0
exit /B 1
Same as above as single command line using conditional execution:
@%SystemRoot%\System32\findstr.exe /L /I /M /C:FAIL "C:\output\summary.txt" >nul && exit /B 1 || exit /B 0
find.exe
executed with /C
outputs the number of lines containing the searched string which can be assigned to an environment variable:
@echo off
set Count=
for /F "tokens=3 delims=:" %%I in ('%SystemRoot%\System32\find.exe /I /C "FAIL" "C:\output\summary.txt" 2^>nul') do set /A "Count=%%I"
if defined Count if %Count% == 0 exit /B 0
exit /B 1
set /A
is used here to eliminate the space character between :
after file name output by find
and the number of lines containing the searched string one or more times. The string after set /A
inside the double quotes is interpreted as arithmetic expression which results in interpreting the space character as delimiter and so after converting the number assigned to loop variable I
with leading space character from string to integer, the number assigned without the space character is assigned as string to environment variable Count
.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded find
command line with using a separate command process started in background.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
exit /?
find /?
findstr /?
for /?
if /?
set /?
I suggest reading also:
I strongly recommend not using:
if %ERRORLEVEL% == 0
if %ERRORLEVEL% EQU 0
if "%ERRORLEVEL%" == "0"
if "%ERRORLEVEL%" EQU "0"
Why?
Well, see what happens on embedding such an exit code evaluation in a command block like copying and pasting into a batch file:
(
set "file=C:\output\summary.txt"
find /C "FAIL" %file%
if "%ERRORLEVEL%"=="0" (
exit /B 1
) else (
exit /B 0
)
)
Run this batch file from within a command prompt window. Is the result right (by chance)? Yes, run it once again without any modification of C:\output\summary.txt
and the batch file. Now the result is the opposite as before although nothing changed. Look on the lines output by Windows command processor and you know why. %ERRORLEVEL%
was replaced by current value of ERRORLEVEL
before find
was executed at all because of cmd.exe
always replaces all environment variable references using syntax %variable%
on parsing entire command block before executing any command left to the command block or inside the command block.