Can a For loop work inside an IF statement?
Moderator: DosItHelp
-
- Posts: 7
- Joined: 17 Dec 2014 16:45
Can a For loop work inside an IF statement?
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.
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.
Re: Can a For loop work inside an IF statement?
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.
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.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Can a For loop work inside an IF statement?
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.
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.
Re: Can a For loop work inside an IF statement?
This code is incorrect if you want to parse what is inside the file because you have quotes around the file name.
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 %%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
-
- Posts: 7
- Joined: 17 Dec 2014 16:45
Re: Can a For loop work inside an IF statement?
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.
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.
Re: Can a For loop work inside an IF statement?
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.
Re: Can a For loop work inside an IF statement?
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!!
Re: Can a For loop work inside an IF statement?
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.
-
- Posts: 7
- Joined: 17 Dec 2014 16:45
Re: Can a For loop work inside an IF statement?
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.