Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file: GOTO in a FOR loop

I have a batch file with a FOR loop. In the loop I must wait for a process to end, for which I used IF and GOTO. The problem is the GOTO is breaking the loop. I tried to find other solutions but I didn't find anything. How can it be done?

@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)
)
like image 596
Leo92 Avatar asked Jun 24 '12 12:06

Leo92


People also ask

How do I use goto in a batch file?

Usage: GOTO can only be used in batch files. After a GOTO command in a batch file, the next line to be executed will be the one immediately following the label. The label must begin with a colon [:] and appear on a line by itself, and cannot be included in a command group.

How do I loop a batch file?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

What is goto EOF in batch file?

In batch code in question the first goto :EOF is needed to exit batch file processing without an unwanted fall through to the subroutine code after finishing the loop. The second goto :EOF in batch code of questioner is for exiting the subroutine and continue processing in FOR loop in second line.


1 Answers

Inside the loop, you could use a call to a subroutine, there are gotos allowed.
The loop will not be broken 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
like image 167
jeb Avatar answered Sep 20 '22 16:09

jeb