The reason for the file as a flag is inter-process communication can be a nightmare and files as flags (or rather dirs as they are created atomically) is universal (i.e. all OS's).
Main Topics
Browse All TopicsIs it possible to have 3 different processes running simultaneously, but wait till all processes are done running before proceeding?
Here is an example:
:: Start Batch File
::Run the following 3 commands simultaneously
Command 1
Command 2
Command 3
::After All 3 commands above are done, do the following:
Command 4
Command 5
:: End Batch File
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's a method I create. First you create the following batch file that I named ProcMark.bat:
@echo off
setlocal
pushd
set markerFile=%~1
del "%markerFile%" 2^>NUL
shift
call %2 %3 %4 %5 %6 %7 %8 %9
popd
echo.>"%markerFile%"
exit
Then in you other batch file you'd say:
@echo off
setlocal
start procmark.bat mark1.txt dir
start procmark.bat mark2.txt dir
start procmark.bat mark3.txt dir
:LOOP
echo Waiting...
for /l %%a in (1, 1, 3) do if not exist mark%%a.txt goto LOOP
echo Done...
REM ** Do other commands
@ECHO OFF
REM If we have a parameter then jump straight to the loader
IF NOT "%1"=="" GOTO Loader
REM We need to know where to watch for completion.
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
MD %WatchPath%
ECHO Looking in %TEMP%\%WatchPath% for completion indicators.
REM Reset the watches
SET Watches=
REM Let's start the various tasks
CALL :PreLoad 1 "C:\WINDOWS\system32\notep
CALL :PreLoad 2 "C:\WINDOWS\system32\calc.
REM Now we wait.
:WatchLoop
SET NotFinished=No
FOR %%A IN (%Watches%) DO IF NOT EXIST %WatchPath%\%%A SET NotFinished=Yes
SLEEP -m 1000
IF "%NotFinished%"=="Yes" GOTO WatchLoop
REM We've finished.
ECHO All tasks completed.
RD /Q /S %WatchPath%
PAUSE
GOTO :EOF
REM Remember what we've started.
:PreLoad
SET Watches=%Watches% %1
REM Load the loader as a separate task
START /Min /Low %~fs0 %1 %~fs2
GOTO :EOF
REM Load the main task and wait for completion.
:Loader
START /WAIT %2
MKDIR %WatchPath%\%1
EXIT
The only external requirement is the freely available Windows Resource Tool Kit for the sleep program.
If you know the tasks are going to take a long time, then increase the SLEEP value.
1000 = 1 second
60000 = 1 minute
for example.
Completing the tasks before that won't make the watcher quit any earlier.
If you don't have the sleep command, the loop takes up too much processing time - try it - your machine will run like a lame duck.
The tool kit is free from http://www.microsoft.com/d
Oops. Typo ...
ECHO Looking in %TEMP%\%WatchPath% for completion indicators.
should be ...
ECHO Looking in %WatchPath% for completion indicators.
Other than that it works OK.
If you want to pass the calling program parameters then here is the corrected code with the notepad opening the batch file! DON'T MAKE ANY CHANGES TO IT WHILST IT IS RUNNING!!!!
@ECHO OFF
REM If we have a parameter then jump straight to the loader
IF NOT "%1"=="" GOTO Loader
REM We need to know where to watch for completion.
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
MD %WatchPath%
ECHO Looking in %WatchPath% for completion indicators.
REM Reset the watches
SET Watches=
REM Let's start the various tasks
CALL :PreLoad 1 "C:\WINDOWS\system32\notep
CALL :PreLoad 2 "C:\WINDOWS\system32\calc.
REM Now we wait.
:WatchLoop
SET NotFinished=No
FOR %%A IN (%Watches%) DO IF NOT EXIST %WatchPath%\%%A SET NotFinished=Yes
SLEEP -m 1000
IF "%NotFinished%"=="Yes" GOTO WatchLoop
REM We've finished.
ECHO All tasks completed.
RD /Q /S %WatchPath%
PAUSE
GOTO :EOF
REM Remember what we've started.
:PreLoad
SET Watches=%Watches% %1
REM Load the loader as a separate task
SET Args=
:AddArg
SET Args=%Args% %3
IF "%4"=="" GOTO GotArgs
SHIFT 3
GOTO AddArg
:GotArgs
ECHO About to run %~fs2 %Args%
START /Min /Low %~fs0 %1 %~fs2 %Args%
GOTO :EOF
REM Load the main task and wait for completion.
:Loader
START /WAIT %2 %3
MKDIR %WatchPath%\%1
EXIT
@ECHO OFF
REM If we have a parameter then jump straight to the loader
IF NOT "%1"=="" GOTO Loader
REM Let's start the various tasks
CALL :PrepareWatch
CALL :PreLoad 1 "C:\WINDOWS\system32\notep
CALL :PreLoad 2 "C:\WINDOWS\system32\calc.
CALL :PreLoad 3 "C:\WINDOWS\system32\taskm
CALL :WatchLoop
CALL :PrepareWatch
CALL :PreLoad 1 "C:\WINDOWS\system32\write
CALL :PreLoad 2 "C:\WINDOWS\system32\sysed
CALL :WatchLoop
GOTO :EOF
:PrepareWatch
REM We need to know where to watch for completion.
MD %WatchPath%
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
ECHO Looking in %WatchPath% for completion indicators.
REM Reset the watches
SET Watches=
GOTO :EOF
REM Now we wait.
:WatchLoop
SET NotFinished=No
FOR %%A IN (%Watches%) DO IF NOT EXIST %WatchPath%\%%A SET NotFinished=Yes
SLEEP -m 1000
IF "%NotFinished%"=="Yes" GOTO WatchLoop
REM We've finished.
ECHO All tasks completed.
RD /Q /S %WatchPath%
GOTO :EOF
REM Remember what we've started.
:PreLoad
SET Watches=%Watches% %1
REM Load the loader as a separate task
SET Args=
:AddArg
SET Args=%Args% %3
IF "%4"=="" GOTO GotArgs
SHIFT 3
GOTO AddArg
:GotArgs
ECHO About to run %~fs2 %Args%
START /Min /Low %~fs0 %1 %~fs2 %Args%
GOTO :EOF
REM Load the main task and wait for completion.
:Loader
START /WAIT %2 %3
MKDIR %WatchPath%\%1
EXIT
Oops. Cut'n'paste error.
MD %WatchPath%
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
should be ...
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
MD %WatchPath%
Sorry.
Also, ...
REM We've finished.
ECHO All tasks completed.
RD /Q /S %WatchPath%
SET WatchPath=
GOTO :EOF
Add the
SET WatchPath=
line
Hopefully, final version.
@ECHO OFF
REM If we have a parameter then jump straight to the loader
IF NOT "%1"=="" GOTO Loader
REM Let's start the various tasks
CALL :PrepareWatch
CALL :PreLoad 1 "C:\WINDOWS\system32\notep
CALL :PreLoad 2 "C:\WINDOWS\system32\calc.
CALL :PreLoad 3 "C:\WINDOWS\system32\taskm
CALL :WatchLoop
CALL :PrepareWatch
CALL :PreLoad 1 "C:\WINDOWS\system32\write
CALL :PreLoad 2 "C:\WINDOWS\system32\sysed
CALL :WatchLoop
GOTO :EOF
:PrepareWatch
REM We need to know where to watch for completion.
MD %WatchPath%
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
ECHO Looking in %WatchPath% for completion indicators.
REM Reset the watches
SET Watches=
GOTO :EOF
REM Now we wait.
:WatchLoop
SET NotFinished=No
FOR %%A IN (%Watches%) DO IF NOT EXIST %WatchPath%\%%A SET NotFinished=Yes
SLEEP -m 1000
IF "%NotFinished%"=="Yes" GOTO WatchLoop
REM We've finished.
ECHO All tasks completed.
RD /Q /S %WatchPath%
SET WatchPath=
GOTO :EOF
REM Remember what we've started.
:PreLoad
SET Watches=%Watches% %1
REM Load the loader as a separate task
SET Args=
:AddArg
SET Args=%Args% %3
IF "%4"=="" GOTO GotArgs
SHIFT 3
GOTO AddArg
:GotArgs
ECHO About to run %~fs2 %Args%
START /Min /Low %~fs0 %1 %~fs2 %Args%
GOTO :EOF
REM Load the main task and wait for completion.
:Loader
START /WAIT %2 %3
MKDIR %WatchPath%\%1
EXIT
Sorry. I get a little excited when I create something for someone else and then realise I can actually use that!
This script is something I've been wanting for myself for ages - a delayed loader during startup.
It's a pain having so many things start all at once when I turn the pc on. By using the script I've just built, I can use it to delay load the various things I want without my machine grinding to a halt trying to load all of them at once.
Ok, not EXACTLY the same script, but the programs I have can be monitored and I can check their state before loading the next set of tasks.
Anyway, slower next time.
Hello,
Your script appears to be working fine. It is giving me a syntax error though.
C:\>batch1.bat
The syntax of the command is incorrect.
Looking in C:\\Mon06182007153916 for completion indicators.
About to run C:\WINDOWS\system32\notepa
About to run C:\WINDOWS\system32\calc.e
About to run C:\WINDOWS\system32\taskmg
Here is the copy paste from the batch file
@ECHO OFF
REM If we have a parameter then jump straight to the loader
IF NOT "%1"=="" GOTO Loader
REM Let's start the various tasks
CALL :PrepareWatch
CALL :PreLoad 1 "C:\WINDOWS\system32\notep
CALL :PreLoad 2 "C:\WINDOWS\system32\calc.
CALL :PreLoad 3 "C:\WINDOWS\system32\taskm
CALL :WatchLoop
CALL :PrepareWatch
CALL :PreLoad 1 "C:\WINDOWS\system32\write
CALL :PreLoad 2 "C:\WINDOWS\system32\sysed
CALL :WatchLoop
GOTO :EOF
:PrepareWatch
REM We need to know where to watch for completion.
MD %WatchPath%
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
ECHO Looking in %WatchPath% for completion indicators.
REM Reset the watches
SET Watches=
GOTO :EOF
REM Now we wait.
:WatchLoop
SET NotFinished=No
FOR %%A IN (%Watches%) DO IF NOT EXIST %WatchPath%\%%A SET NotFinished=Yes
SLEEP -m 1000
IF "%NotFinished%"=="Yes" GOTO WatchLoop
REM We've finished.
ECHO All tasks completed.
RD /Q /S %WatchPath%
SET WatchPath=
GOTO :EOF
REM Remember what we've started.
:PreLoad
SET Watches=%Watches% %1
REM Load the loader as a separate task
SET Args=
:AddArg
SET Args=%Args% %3
IF "%4"=="" GOTO GotArgs
SHIFT 3
GOTO AddArg
:GotArgs
ECHO About to run %~fs2 %Args%
START /Min /Low %~fs0 %1 %~fs2 %Args%
GOTO :EOF
REM Load the main task and wait for completion.
:Loader
START /WAIT %2 %3
MKDIR %WatchPath%\%1
EXIT
Just to elaborate on the usage of this line:
start procmark.bat mark1.txt dir
Procmark.bat accepts a marker file name and the command to run. In this case the marker file is named mark1.txt and the command is the dir command. The command could easily been an executable or another batch file with or without parameters specified. The code allows for up to 7 parameters to be passed to the procmark.bat's command portion.
Hi guys, seeing as your using DOS Batch, this is probably more just FYI, but I have created a VBScript that can control such issues, along with a batch file that calls it.
I wrote a vb script that takes vbs files as parameters, and keeps running until the passed scripts have finished. This way, it returns back to the START /WAIT command that called it.
I have called this script RunAndWait.vbs"
'=========================
'MUST BE RUN BY: START /WAIT RunAndWait.vbs script1.vbs script2.vbs
If WScript.Arguments.Count > 0 Then
strWhileCondition = "Do Until "
Set wshShell = CreateObject("WScript.Shel
For intArgNum = 0 To WScript.Arguments.Count - 1
strCodeToExec = "Set objExec" & intArgNum & " = wshShell.Exec(""wscript.ex
Execute strCodeToExec
If intArgNum < WScript.Arguments.Count - 1 Then
strWhileCondition = strWhileCondition & "objExec" & intArgNum & ".Status OR "
Else
strWhileCondition = strWhileCondition & "objExec" & intArgNum & ".Status"
End If
Next
strWhileCondition = strWhileCondition & VbCrLf & "WScript.Sleep 500" & VbCrLf & "Loop"
Execute strWhileCondition
End If
'=========================
And in Master.bat I have these lines:
@echo off
start /wait RunAndWait.vbs file1.vbs file2.vbs file3.vbs
start /wait RunAndWait.vbs file4.vbs
What this does is run 1, 2, and 3 concurrently, and the vbs uses dynamic code through the Execute statement to monitor all three Exec processes. Then when the RunAndWait.vbs script has finished, the batch file kicks in again, and runs file4.
Basically, I figure that in the Master.bat, you place a RunAndWait.vbs statement for each "block" of scripts that you are allowed to run concurrently.
Now, given that you are running "commands" and not "files", this doesn't suit exactly, *but* you could alter the RunAndWait.vbs to process "commands" that were passed to it, which it would execute in separate command prompts.
Anyway, that's just my two cents.
Regards,
Rob.
Oops. Another typo.
Replace the :PrepareWatch code with this ...
:PrepareWatch
REM We need to know where to watch for completion.
FOR /F "tokens=1-7 delims=:\/. " %%A IN ("%DATE% %TIME%") DO SET WatchPath=%TEMP%\%%A%%B%%C
MD %WatchPath%
ECHO Looking in %WatchPath% for completion indicators.
REM Reset the watches
SET Watches=
GOTO :EOF
I STILL had the MD in the wrong place. Sorry.
Don't forget the SLEEP program from Windows Resource Tool Kit. Oh. My PATH has the path to the tool kit too as this means I don't need to use C:\Progra~1\WI8DE7~1\Tools
Not being funny or anything, and I love you gorgeous code... but.. seriously overkilled.... You can do simultaneous copys with a simple switch.. Start.
For example. I wanted to clone 8 USB drives at a time from a pre-set folder on my desktop.. I created this
---BATCH FILE---
robocopy c:\USBClone\Files d: /mir
robocopy c:\USBClone\Files e: /mir
robocopy c:\USBClone\Files f: /mir
robocopy c:\USBClone\Files g: /mir
robocopy c:\USBClone\Files h: /mir
robocopy c:\USBClone\Files i: /mir
robocopy c:\USBClone\Files j: /mir
robocopy c:\USBClone\Files k: /mir
----END BATCH FILE---
As you know this will do the copy one after another... I wanted the process to be quicker so I added the Start switch to run all commands at the same time. So.. New batch file is now as follows
---START BATCH FILE---
Start robocopy c:\USBClone\Files d: /mir
Start robocopy c:\USBClone\Files e: /mir
Start robocopy c:\USBClone\Files f: /mir
Start robocopy c:\USBClone\Files g: /mir
Start robocopy c:\USBClone\Files h: /mir
Start robocopy c:\USBClone\Files i: /mir
Start robocopy c:\USBClone\Files j: /mir
Start robocopy c:\USBClone\Files k: /mir
----END BATCH FILE---
Simple. easy to read. Not overkill at all.. In your case;
---START BATCH FILE---
::Run the following 3 commands simultaneously
Start Command 1
Start Command 2
Start Command 3
::After All 3 commands above are done, do the following (To make these simultaneous add start before them.):
Command 4
Command 5
---END BATCH FILE---
I hope that helps. I just saved myself £800 on a USB duplicator by making it simple!!
We went in another direction on this project because we could not have more than 1 connection to the server we were trying to use this for. You guys did a lot of good work here so I want to assign the points. I will ask you to decide who gets the points and I will award accordingly.
Thanks for taking the time to answer and I am sorry it took me so long to get back to you.
Business Accounts
Answer for Membership
by: RQuadlingPosted on 2007-06-18 at 07:15:06ID: 19307211
You can. The simplest way is to use something like a file to act as a flag.
Each of the commands would be another batch file (or the same one!)
Here is an example where I will open notepad and calc at the same time and wait for both of them to finish.