My requirement is simple, i just want to scan all the files in the current directory for a particular string and if that string is found i just want a display saying "String is found" otherwise "String not found"
@ECHO OFF
for %%f in (C:\Users\aalvoor\Desktop\BatchScript\*) do (
echo File is %%f
find /c "defaultModel" %%f >NUL
if %errorlevel% equ 1 (echo File is notfound) else (echo String is found)
)
But the problem is it works when i am not putting it in a for loop but when i put it in for loop for some reason for every file i get a message String is found which is not true.
if /?and read the output help which explains already on first page the recommended syntax to evaluate the exit code of a former run command or executable. So useif errorlevel 1instead ofif %errorlevel% equ 1and the code works and you have not to think about delayed expansion. See also single line with multiple commands using Windows batch file and chapter 4 of this answer about dynamic variables.