Can a For loop work inside an IF statement?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
williamt31
Posts: 7
Joined: 17 Dec 2014 16:45

Can a For loop work inside an IF statement?

#1 Post by williamt31 » 17 Dec 2014 16:56

I have a script that I love, works well but of course I like improving things.

I recently learned how to put some error catching in my script but it meant changing from a GOTO structure to a IF list. Below is a simplified stretch showing just the option I'm working on. I do have 'setlocal EnableDelayedExpansion' at the top of my script already.

SET /P C="Choice" :
GOTO %C%

:3
Do

to

IF %C% == 3 ( DO )

Old Block
:3
REM Clean computers all at once
For /F %%i in (.\Clean_PC_List.txt) do start .\Win7_Classsroom_Clean.cmd %%i
goto menu

New Block
IF %C% == 2 (
For /F %%i in (".\Clean_PC_List.txt") do start ".\Win7_Classroom_Clean.cmd" %%i )
GOTO menu

Old Block works perfect because it runs a specific script with multiple actions on each PC name in the list provided.

I've tried playing with the %%i I've tried switching () {} and a few other things.

Any advice? If you need more of the script I can try to post more but it's over 250 lines long at the moment and most of it wouldn't apply.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Can a For loop work inside an IF statement?

#2 Post by Squashman » 17 Dec 2014 17:08

Not exactly sure which line of code you are questioning?

What version of Windows are you using?
If you are using Windows Vista and above then you can get rid of the SET /P and error checking and replace it with the CHOICE command.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Can a For loop work inside an IF statement?

#3 Post by ShadowThief » 17 Dec 2014 18:21

What's not working?

Having a for loop inside of an if statement is perfectly valid. Really the only thing I see "wrong" with your code is that you've got spaces around the ==, which will cause unexpected behavior since batch will include the spaces in the comparison when it shouldn't. Also, conventionally, the ) goes on its own line at the end of the if block, but it shouldn't break anything.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Can a For loop work inside an IF statement?

#4 Post by Squashman » 17 Dec 2014 18:21

This code is incorrect if you want to parse what is inside the file because you have quotes around the file name.

Code: Select all

For /F %%i in (".\Clean_PC_List.txt") do start ".\Win7_Classroom_Clean.cmd" %%i )


IF you have quotes around the file name then I would advise you start using the USEBACKQ option and I always make it a habit to specify TOKENS=* or DELIMS= nothing.

Code: Select all

For /F "usebackq delims=" %%i in (".\Clean_PC_List.txt") do start ".\Win7_Classroom_Clean.cmd" %%i

williamt31
Posts: 7
Joined: 17 Dec 2014 16:45

Re: Can a For loop work inside an IF statement?

#5 Post by williamt31 » 17 Dec 2014 19:35

ShadowThief

I've seen it both ways, I tried it without and with. (In reguards to the spaces with the equal signs, I also tried 'EQU' at one point but I can try it again and remove spaces when I get back to work tomorrow.)

The lines under 'Old Block' works I was just trying to do a capture of any input not in the menu and the only way I figured out how to do it was IF with an ELSE at the end routing to an Error block.

Squashman, I don't know why but I couldn't sworn I tried to do something with 'choice' years ago and kept finding that it was for older windows not newer but it's on my Win7 box at home so it must be on my box at work too. I'll re-write the script tomorrow as apparently choice does exactly what I want including error catching. (I didn't even know I had a PC speaker plugged in on my PC) Only catch is only 9 options per menu but that's fine as I had already split my menu in half because it was getting cluttered.

I am curious though why my 'IF xx xx ( FOR' block won't work though. if anyone else has any ideas, just because it's bugging me though.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Can a For loop work inside an IF statement?

#6 Post by Squashman » 17 Dec 2014 20:16

I mentioned in my previous post what was wrong with your FOR /F command and it doesnt look like you are comparing the same value. You used a 3 the first time and then a 2 the second time.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Can a For loop work inside an IF statement?

#7 Post by Squashman » 17 Dec 2014 20:39

williamt31 wrote:If you need more of the script I can try to post more but it's over 250 lines long at the moment and most of it wouldn't apply.

That's nothing! Take a look at Dbenham's JREPL.bat. That is 300 lines with just the documentation!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Can a For loop work inside an IF statement?

#8 Post by foxidrive » 18 Dec 2014 00:09

williamt31 wrote:I am curious though why my 'IF xx xx ( FOR' block won't work though.


It looks like you are getting input and branching to whatever is in the input.

The obvious problem is that the input is not being validated and all sort of things being typed can break the next line.

williamt31
Posts: 7
Joined: 17 Dec 2014 16:45

Re: Can a For loop work inside an IF statement?

#9 Post by williamt31 » 18 Dec 2014 10:07

Squashman wrote:I mentioned in my previous post what was wrong with your FOR /F command and it doesnt look like you are comparing the same value. You used a 3 the first time and then a 2 the second time.


I was cleaning and re-arranging at the same time. I'm working with a script that at least 2 people created and touched before me. I'm one for streamlining and adding as much as I can while still keeping it as clean as possible.

I wish I'd known that choice was still in Windows. I can't believe how many google searches I've done in weeks past and everyone has their own nifty way of doing a menu but unless you search for 'errorlevel' even just 'choice' you get people that name the prompt and are not using the command...

Choice /C 12345678ECFNAHVQL /N /M "Select Option From Above:"
GOTO %ERRORLEVEL%

Finished product, works like a champ. I also learned a really cool thing this morning, choice keeps counting so I just keep my GOTO incrementing to option :17 and it all works.

Post Reply