等待命令完成

发布于 2024-11-28 02:45:53 字数 93 浏览 3 评论 0原文

我有一个启动其他批处理文件的 Windows 批处理文件。在执行第一批中的某些内容之前,如何等待所有其他批处理文件完成?我无法使用 /wait 因为我需要其他命令并行运行。

I have a Windows batch file that starts other batch files. How do I wait for all the other batch files to finish before executing somthing in the first batch? I can't use /wait because I need the other commands to run in parallel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹泡泡o 2024-12-05 02:45:53

您可以使用多个标志文件,每个正在运行的批处理文件一个。例如:

第一个批处理文件在输入时创建Flagfile.1,并在结束之前将其删除,并且以同样的方式其余并发批处理文件(Flagfile.2,...)

主文件必须等待所有flagfile消失。例如,在主文件中:

start Batch1
start Batch2
.....
:wait
if exist flagfile.* goto wait

在任何批处理文件中,例如 Batch1:

echo %time% > flagfile.1
echo Do my process...
del flagfile.1
exit

You may use several flag files, one per running Batch file. For example:

First batch file create Flagfile.1 when enter, and delete it before end, and the same way the rest of concurrent batch files (Flagfile.2,...)

The main file just must wait for all flagfiles disappear. For example, in Main file:

start Batch1
start Batch2
.....
:wait
if exist flagfile.* goto wait

In any Batch file, for example Batch1:

echo %time% > flagfile.1
echo Do my process...
del flagfile.1
exit
花开柳相依 2024-12-05 02:45:53

我的建议是使用 CALL 命令。 批处理文件中的示例:

Batch1.bat 列表:

REM Batch1.bat
SET ABC=1
CALL BATCH2.BAT %ABC%
ECHO ABC = %ABC%
BATCH2.BAT %ABC%
ECHO ABC = %ABC%

其中 Batch2.bat 列表为:

REM Batch2.bat
SET ABC=%ABC%%1


EDIT: based on feedback from @Andriy M, here is an improved version for the two batches:

第 1 批:

@ECHO OFF
REM Batch1.bat
SET ABC=1
CALL batch2.bat %ABC%
ECHO 1. ABC = %ABC%
CALL batch2.bat %ABC% REM test this line with CALL and without
ECHO 2. ABC = %ABC%

第 2 批:

@ECHO OFF
REM Batch2.bat
SET tmout=5
echo sleeping %tmout% seconds...
REM this introduces a timeout by using a nonexistent ip address
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
echo done sleeping
SET ABC=%ABC%%1

请参阅第 1 批中的行,其中我写了注释使用 CALL 测试此行,不使用 CALL。运行该批处理两次,该行有 CALL 和没有 CALL

控制台的输出不带 CALL

C:\temp>batch1.bat
sleeping 5 seconds...
done sleeping
1. ABC = 11
sleeping 5 seconds...
done sleeping

现在控制台的输出 CALL

C:\temp>batch1.bat
sleeping 5 seconds...
done sleeping
1. ABC = 11
sleeping 5 seconds...
done sleeping
2. ABC = 1111

请注意区别:我们得到第二个回声,qed

My suggestion is to use CALL command. An example from Batch Files:

Batch1.bat listing:

REM Batch1.bat
SET ABC=1
CALL BATCH2.BAT %ABC%
ECHO ABC = %ABC%
BATCH2.BAT %ABC%
ECHO ABC = %ABC%

where Batch2.bat listing is:

REM Batch2.bat
SET ABC=%ABC%%1


EDIT: based on feedback from @Andriy M, here is an improved version for the two batches:

Batch 1:

@ECHO OFF
REM Batch1.bat
SET ABC=1
CALL batch2.bat %ABC%
ECHO 1. ABC = %ABC%
CALL batch2.bat %ABC% REM test this line with CALL and without
ECHO 2. ABC = %ABC%

Batch 2:

@ECHO OFF
REM Batch2.bat
SET tmout=5
echo sleeping %tmout% seconds...
REM this introduces a timeout by using a nonexistent ip address
PING 1.2.1.2 -n 1 -w %tmout%000 > NUL
echo done sleeping
SET ABC=%ABC%%1

See the line in batch1 where I wrote the comment test this line with CALL and without. Run that batch twice, with that line having CALL and without CALL.

The output from console without CALL:

C:\temp>batch1.bat
sleeping 5 seconds...
done sleeping
1. ABC = 11
sleeping 5 seconds...
done sleeping

And now the output from console with CALL:

C:\temp>batch1.bat
sleeping 5 seconds...
done sleeping
1. ABC = 11
sleeping 5 seconds...
done sleeping
2. ABC = 1111

Please note the difference: we get the 2nd echo, q.e.d.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文