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

吃可爱长大的小学妹 提交于 2021-01-29 10:09:25

问题


I have a series of bat files that I want to run in parallel, for example:

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? Thanks,

EDIT: This is the content of program1_i.bat file:

program1.exe input_i.txt

It will run program1.exe for each input file. The same for program2_i.bat.


回答1:


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




回答2:


* 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.

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.

@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



回答3:


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



回答4:


Try /WAIT switch on start command. I guess if you wrap bats into script called with wait switch, it could work.



来源:https://stackoverflow.com/questions/59729547/run-bat-files-in-parallel-and-wait-until-they-are-finished-before-run-new-set-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!