简体   繁体   English

并行运行 bat 文件并等待它们完成,然后再运行新的 bat 文件集

[英]Run bat files in parallel and wait until they are finished before run new set of bat files

I have a series of bat files that I want to run in parallel, for example:我有一系列要并行运行的 bat 文件,例如:

start program1_1.bat
start program1_2.bat
start program1_3.bat
wait until program1 finished then
start program2_1.bat
start program2_2.bat
start program2_3.bat
wait until program2 finished then
...

So far, what I've tried is this function :到目前为止,我尝试过的是这个功能

:waitForFinish
set counter=0
for /f %%i in ('tasklist /NH /FI "Imagename eq cmd.exe') do set /a counter=counter+1
if counter GTR 2 Goto waitForFinish

But it just launched the first 3 bat files and stop... How can I solve this problem?但它只是启动了前3个bat文件并停止了......我该如何解决这个问题? Thanks,谢谢,

EDIT: This is the content of program1_i.bat file:编辑:这是program1_i.bat文件的内容:

program1.exe input_i.txt

It will run program1.exe for each input file.它将为每个输入文件运行 program1.exe。 The same for program2_i.bat . program2_i.bat

Your question is a little vague on the exact expected results.您的问题对确切的预期结果有点含糊。

I am however assuming that you want to do something like this.然而,我假设你想做这样的事情。

start /wait program1_1.bat | start /wait program1_2.bat | start /wait program1_3.bat
start /wait program2_1.bat | start /wait program2_2.bat | start /wait program2_3.bat

The single pipe separators let's us launch the first three commands in parallel and only start the next three commands once the first three has all completed, simply because the next 3 commands are in the next batch line the use of start /wait单管道分隔符让我们并行启动前三个命令,并且只有在前三个命令都完成后才启动接下来的三个命令,仅仅因为接下来的 3 个命令在下一个batch行中使用start /wait

* Update * The solution provided here works nicely to achieve parallel running of subprograms without needing user entry (pause) or a fixed length Timeout between program groups. *更新*这里提供的解决方案可以很好地实现子程序的并行运行,而无需用户输入(暂停)或程序组之间的固定长度超时。

Combined with the original answer:结合原答案:

@echo off

:controller

Call :launcher "program1_1.bat" "program1_2.bat" "program1_3.bat"

Call :launcher "program2_1.bat" "program2_2.bat" "program2_3.bat"

pause
EXIT

:launcher
For %%a in (%*) Do (
Start "+++batch+++" "%%~a"
)

:loop
    timeout /t 1 >nul
    tasklist /fi "windowtitle eq +++batch+++*" |find "cmd.exe" >nul && goto :loop

GOTO :EOF

Original Answer:原答案:

This is a simple way to ensure each group of programs is finished before the next.这是确保每组程序在下一组程序之前完成的简单方法。 The fault in the tasklist method is that if there's other cmd.exe processes running, the If condition may not be true when expected, causing the script to hang. tasklist 方法的错误在于,如果有其他 cmd.exe 进程正在运行,则 If 条件可能不符合预期,从而导致脚本挂起。

The start /wait option is not ideal, as you intend on running multiple programs simultaneously - and if the subprogram you wait on finishes before the other subs, you're back to square 1. start /wait 选项并不理想,因为您打算同时运行多个程序 - 如果您等待的子程序在其他子程序之前完成,您将回到第 1 方格。

@echo off

:controller

Call :launcher "program1_1.bat" "program1_2.bat" "program1_3.bat"

Call :launcher "program2_1.bat" "program2_2.bat" "program2_3.bat"

pause
EXIT

:launcher
For %%a in (%*) Do (
Start "" "%%~a"
)

pause
GOTO :EOF

Ok, here is what worked for me:好的,这对我有用:

@echo off
setlocal enableextensions enabledelayedexpansion
call :InitDos
start program1_1.bat
start program1_2.bat
start program1_3.bat
call :waitForFinish
start program2_1.bat
start program2_2.bat
start program2_3.bat
call :waitForFinish
Goto:eof
: waitForFinish
set /a counter=0
for /f %%i in ('tasklist /NH /FI "Imagename eq cmd.exe"') do (
    set /a counter+=1
)
if !counter! GTR !init_count! Goto waitForFinish
goto :eof
: InitDos
set /a init_count=0
for /f %%i in ('tasklist /NH /FI "Imagename eq cmd.exe"') do (
    set /a init_count+=1
)
goto :eof

Try /WAIT switch on start command.start命令上尝试/WAIT开关。 I guess if you wrap bats into script called with wait switch, it could work.我想如果您将蝙蝠包装到使用等待开关调用的脚本中,它就可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM