If then statement in the for loop

I'm nebiew for windows scripting. My scripts would seachs string in the file, if counted the string and checking return code in if then else statment. But, I have question, some error on if then statment show [%i] was unexpected in this time.

@echo off
cd C:\script\
for /f "tokens=3" %%i in ( 'find /C "ANR2034E" checkout.txt' ) do ( echo %%i )
if [/I] [%%i] == [1] ( echo %%i ) else ( echo No ) )

Furthermore, May I integrate if statement in the for loop?

example:

for %%i in ('command') do ( if [/I] [%%i] == [i] (command) else (command))

1 Answer

@echo off
cd C:\script\
for /f "tokens=3" %%i in ('find /C "ANR2034E" checkout.txt') do ( echo %%i if [%%i]==[1] ( echo %%i ) else ( echo No )
)

The error was because you had /i after the if within square brackets. In any case /i (case insensitivity) is not required because you've used find /c which will return only an integer.

As you can see, the if condition can definitely be included within the for loop.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like