Batch command to find number pof occurrences of a string in a file


I need to find how many times a string exists in a file. If it is not equal to 0 then return error status:

set "file=C:\output\summary.txt"
for /f %%F in (findstr /I "FAIL" %file% | find /I /C "FAIL") do (
    set count=%%F

if count neq 0 exit /B 1
exit /B 0

But when I run the batch I get error:

| was unexpected at this time.

How can I fix the issue and achieve the expected?


That seems very complicated when the find command does what you need (it sets %ERRORLEVEL% to 1 if it doesn’t find the string):

set "file=C:\output\summary.txt"
find /C "FAIL" %file%
if "%ERRORLEVEL%"=="0" (
    exit /B 1
) else (
    exit /B 0

Answered By – thebjorn

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published