批处理文件:在 FOR 循环中转到 [英] Batch file: GOTO in a FOR loop

查看:44
本文介绍了批处理文件:在 FOR 循环中转到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 FOR 循环的批处理文件.在循环中,我必须等待一个进程结束,为此我使用了 IF 和 GOTO.问题是 GOTO 正在打破循环.我试图找到其他解决方案,但我什么也没找到.怎么办?

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)
)

推荐答案

在循环内部,您可以对子程序使用call,允许使用goto.
对子程序的调用不会破坏循环.

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

这篇关于批处理文件:在 FOR 循环中转到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆