1

Objective: 1. Run two batch files in parallel 2. Execute third file Post completion of (1.)

What i have done?

1- Created a batch file Pr1.bat with following commands

cd Directory
START CScript "X.vbs"
START CScript "Y.vbs"

2- Created a batch file Pr2.bat with following commands

cd Directory
START CScript "Z.vbs"

3- Create a master batch file master.bat with following commands

cd Directory (same as above directory)
CALL Pr1.bat
CALL Pr2.bat
PAUSE

Now when i execute the master batch file, all three .vbs script gets triggered simultaneously. What i want to accomplish is to wait for the Y.vbs csript process to finish off before returning control to the master file.

How do i make batch file wait for the cscript to finish off and then execute the next call functions?

user692942
  • 16,398
  • 7
  • 76
  • 175
user3338307
  • 21
  • 2
  • 5

1 Answers1

3

Change the Pr1.bat file this way:

cd Directory
(
START CScript "X.vbs" 
START CScript "Y.vbs" 
) | pause

For a detailed explanation of this method, see this answer.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • This technique works here, but it relies on a complex set of factors. – Eryk Sun May 12 '17 at 13:39
  • cmd.exe doesn't use the `CreateProcess` `STARTUPINFO` fields to explicitly inherit standard handles. Instead it relies on temporarily modifying its own standard handles before spawning a child. Due to undocumented behavior in `CreateProcess`, this allows console programs that inherit the current console to inherit the current standard handles, whatever they are. It even works when cmd uses `ShellExecuteEx`, which normally doesn't inherit handles, but, again, this is special behavior for a console app that inherits the current console. – Eryk Sun May 12 '17 at 13:41
  • `start` defaults to creating a new console, so the implicit inheritance of standard handles doesn't occur. That said, when cmd calls `CreateProcess` it passes `bInheritHandles` as `TRUE` all the same. In this case each child on the left-hand side (the writing side) of the pipe inherits a handle to the pipe, but it's not set as the `StandardOutput` for the process, and the process has no knowledge of this handle. It won't be closed until the process exits. Thus `pause` on the read end will wait for all possible writers to exit. – Eryk Sun May 12 '17 at 13:43
  • If we instead ran "X.vbs" directly, relying on the file association to run cscript.exe or wscript.exe, cmd would have to use `ShellExecuteEx` instead of `CreateProcess`. This time the trick won't work, because under the hood `ShellExecuteEx` calls `CreateProcess` with `bInheritHandles` as `FALSE`. So the cscript or wscript process won't inherit the pipe handle. This technique will also fail if the target program is itself just a launcher for a process that gets created without inheriting the pipe handle. – Eryk Sun May 12 '17 at 13:44