简体   繁体   中英

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?

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.

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM