I am trying to make a loop that will go through a set of text files in specified directory searching for a string. The result is reported based on whether the string is found. But the %errorlevel% always returns 0 and evaluates to 0.
SETLOCAL enabledelayedexpansion
FOR %%G IN (*.txt) DO (
find /i "My text string" "%%G"
ECHO %date% %time% : errorlevel is %errorlevel% >> %report_dir%\%computername%.txt
IF %errorlevel% EQU 1 (
ECHO %date% %time% : String found >> %report_dir%\%computername%.txt
GOTO:copy_log
)
)
ENDLOCAL
Raymond did you mean that?:
SETLOCAL enabledelayedexpansion
FOR %%G IN (*.txt) DO (
find /i "My text string" "%%G"
IF %errorlevel% (
ECHO %date% %time% : String found >> %report_dir%\%computername%.txt
GOTO:copy_log
)
)
ENDLOCAL
I prefer to make
Forloops call branches. It prevents variable expansion problems: