Check the string is exist or not in a file and use it in the Windows Batch file

Summary

Do something in the Windows Batch file depend on that some string is exist or not in a file.

Batch file

We'll find string "sample" in the file sample.txt.

@echo off
find "sample" sample.txt
echo ERRORLEVEL=%ERRORLEVEL%

if %ERRORLEVEL% == 0 (
    echo string is not found.
) else (
    echo string is found.
)

Output

When the file sample.txt is include string "sample".

---------- SAMPLE.TXT
This is a sample file.
ERRORLEVEL=0
string is found.

*The find command output file name and found line at first 2 lines of output.

When the file sample.txt is not include string "sample".

---------- SAMPLE2.TXT
ERRORLEVEL=1
string is not found.

*The find command output file name at first line of output.