batch file : goto in for loop

Leo92 picture Leo92 · Jun 24, 2012 · Viewed 33k times · Source

I have a batch file that have a for loop in it , in the loop I must wait for process to end so I use IF and GOTO , the problem is the goto is breaking the loop, I tried to find other solutions but I didnt get anything , my code is doing a loop then I check for a process running , if prog.exe is not runnig then continue the loop , but I dont want to break the main for loop, is there a solution ? or any alternative ?

@echo off
for /f "tokens=*" %%a in (file.txt) do (
bla bla bla
bla bla bla
:check
tasklist /FI "IMAGENAME eq prog.exe" 2>NUL | find /I /N "prog.exe">NUL
if "%ERRORLEVEL%"=="0" (goto check)
)

Answer

jeb picture jeb · Jun 24, 2012

Inside the loop, you could use a call to a subroutine, there are goto's allowed.
The loop will not be breaked by a call to a subroutine.

@echo off
for /f "tokens=*" %%a in (file.txt) do (
  bla bla bla
  bla bla bla
  call :check
)
exit /b

:check
tasklist /FI "IMAGENAME eq prog.exe" 2>NUL | find /I /N "prog.exe">NUL
if "%ERRORLEVEL%"=="0" (goto check)
exit /b