there,
I have two batch files: BatchA.bat
, BatchB.bat
BatchA.bat
is calling B1.bat
, B2.bat
, B3.bat
and runs the three batch files at the same time (the three batch files' running order does not matter).
this is BatchA.bat
:
start B1.bat
start B2.bat
start B3.bat
BatchB.bat
is calling B4.bat
, B5.bat
, B6.bat
and runs the three batch files at the same time (the three batch files' running order does not matter).
this is BatchB.bat
:
start B4.bat
start B5.bat
start B6.bat
I am using C# console application to call BatchA.bat
, BatchB.bat
, but I need to make sure BatchB.bat
won't start until BatchA.bat
if finished. In other words, I need to make sure all B1.bat
, B2.bat
and B3.bat
complete before I start BatchB.bat
This is the C# code:
proc.StartInfo.FileName = BatchA.bat;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();
Console.WriteLine("Process Complete! exitCode: " + exitCode.ToString());
proc.StartInfo.FileName = BatchB.bat;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
exitCode = proc.ExitCode;
proc.Close();
Console.WriteLine("Process Complete! exitCode: " + exitCode.ToString());
When I run this, all 6 batch files start at the same time. How do I make sure BatchA.bat
is not complete until all three small batch files are complete?
A simple way of doing it could be to pipe the start
commands. When the child processes end, the left part of the pipe will be closed, closing the full pipe and batch execution will continue
So, batchA.cmd
could be coded as
@echo off
setlocal enableextensions disabledelayedexpansion
echo starting BatchA
(
start "b1" cmd /c b1.cmd
start "b2" cmd /c b2.cmd
start "b3" cmd /c b3.cmd
) | more
echo BatchA has ended
note: this method, while simple, can have a drawback (or not, it depends on the needs). Processes started by the b1
...b3
can keep the pipe active.
@Squashman has already posted the code to handle not waiting for the processes started from inner batch files.
You could use WAITFOR
. In Batch A, use this:
@echo off
waitfor done
waitfor done
waitfor done
echo done
Then in B1, B2, B3, use WAITFOR /SI done
when the batch is done running.
Alternatively, you could create a 0-byte marker file in each of B1, B2, and B3 as a monitor, checking for the existence of any before ending Batch A, but is probably more complicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With