Ajesh
Ajesh

Reputation: 59

Not getting the required output through "find" command in a for loop in Windows Batch Scripting

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.

Upvotes: 0

Views: 56

Answers (1)

user7818749
user7818749

Reputation:

Use conditional operators && and ||

&& being if errorlevel equ 0

|| being if errorlevel neq 0

@echo off
for %%f in ("%userprofile%\Desktop\BatchScript\*") do (
    echo File is %%f
    find /c "defaultModel" %%f >nul 2>&1 && echo String found || echo String NOT found
)

Upvotes: 1

Related Questions