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)
)
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.
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.
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.
Inside the loop, you could use a call
to a subroutine, there are goto
s 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With