I have a main batch file than calls 4 other batch files so we can run in parallel.
Example:
Main.bat
start call batch1.bat
start call batch2.bat
start call batch3.bat
start call batch4.bat
exit
I want the Main.bat to exit after all the batch1 to batch 4 has stopped executing. In this way, I can get the total run time of the batch file. Problem is Main.bat exits even before batch1 to batch4 finishes executing.
I tried to compute for %errorlevel% for each batch file, but it always return 0 even though the 4 .bat files are still running.
Hoping someone could help me!
Thank you! :)
You could have batch1..batchn4 create flag files when they finish running.
e.g
echo y > flag1
in batch1.batThen in the main batch file check for the existence of the flag files before exiting. You would need some sort of sleep utility to do something like this at the end of the main batch file:
The downside of this technique is that your timing is going to be off a little because you're polling for the flag files instead of being event driven. This may or may not be a problem in your situation. Batch files are pretty limited in this way. You might be better off trying to do it in PowerShell or python or some other more capable scripting language.
Give this a try.
I think this is the simplest and most efficient way:
In this method the waiting state in the main file is event driven, so it does not consume any CPU time!
EDIT: Some explanations added
The
set /P
command would terminate when anyone of the commands in the( block )
outputs a line, butstart
commands don't show any line in this cmd.exe. This way,set /P
keeps waiting for input until all processes started bystart
commands ends. At that point the pipe line associated to the( block )
is closed, so theset /P
Stdin is closed andset /P
command is terminated by the OS.give a unique title string to the new processes, then check if any processes with this string in the title are running:
(
find
is used, becausetasklist
does not return a helpful errorlevel)