goto within for loop is not working

July 6, 2010 at 00:14:18
Specs: Windows XP
i tried the following in a.bat

for %%f in (mayank,anurudh) do (
c:
cd\%%f
if errorlevel 1 goto :cont2
echo %%f
goto :cont3
:cont2
echo error %%f
:cont3
rem

)
this does not work
on errorlevel 1 it echos %f not mayank or anurudh
can anyone explain why and how to rectify


See More: goto within for loop is not working

Report •


#1
July 6, 2010 at 02:36:07
bnvenkataraman, you'll likely get more and more knowledgeable responses in the programming forum @:
http://www.computing.net/forum/prog...
HTH.
Ed in Texas.

Report •

#2
July 6, 2010 at 05:07:04
Goto will break a for loop (with the notable exception of for /L); it will stop iterating entirely. you can either call a label/subroutine or expand the if statement.


for %%f in (mayank,anurudh) do (
c:
cd\%%f
if errorlevel 1 (
    echo error %%f
) 
.......
)

or

for %%f in (mayank,anurudh) do (
c:
cd\%%f
if errorlevel 1 call :cont2
............
)
goto :eof

:cont2
echo error %%f
goto :eof


edit: I should say for /L will continue iterating but not execute......


Report •

#3
July 6, 2010 at 06:01:13
Got your point Thank you judago

Report •

Related Solutions

#4
July 6, 2010 at 06:45:49
No problem!

I should note that I wasn't paying much attention, "echo %%f" wouldn't work(outside the scope of for) in the subroutine, but you can pass the data as a parameter:

for %%f in ..... do call :blah %%a
goto :eof

:blah
echo %*
goto :eof


Report •

#5
July 6, 2010 at 06:58:25
Thank you once again Mr/Ms judago
i tried your previous call it did not work
i try this one
thank you once again
i face similar problem for
set /p evar="what is your input"
within for loop
that also exits loop
only some counter can work within for loop i hope so
thanks

Report •

#6
July 6, 2010 at 07:22:59
thank u mr/ms judago second suggestion is working

Report •

#7
July 6, 2010 at 08:06:10
I can guess what the second issue was %var% wasn't expanding properly inside the loop, delayed expansion + !var! generally works:

SetLocal EnableDelayedExpansion
for %%a in (blah) do (
    set /p var=???
    echo !var!
    pause
)

http://judago.webs.com/variablecatc...

It's, "mr" judago ;).


Report •


Ask Question