Hauari
Hauari

Reputation: 43

make many process wait before the main process

I have this code:

Start "" robocopy F:\DATA1 D:\Backup\  
Start "" robocopy G:\DATA2 D:\Backup\    
Start "" robocopy H:\DATA3 D:\Backup\    
Start "" D:\Backup\rename.bat

How to make the rename.bat WAIT all process before execute?

Upvotes: 0

Views: 2405

Answers (3)

Magoo
Magoo

Reputation: 79982

@ECHO OFF
SETLOCAL
:: first method - use flag files (qtest.bat substituted for robocopy)
:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")
COPY NUL "%tempfile%b">nul&START "" qtestd "%tempfile%b" f:\data1 d:\backup\
COPY NUL "%tempfile%c">nul&START "" qtestd "%tempfile%c" g:\data2 d:\backup\
COPY NUL "%tempfile%d">nul&START "" qtestd "%tempfile%d" h:\data3 d:\backup\
del "%tempfile%a"
:wait
timeout /t 1 >nul
FOR %%a IN (b c d) DO IF EXIST "%tempfile%%%a" GOTO wait
ECHO Start "" D:\Backup\rename.bat
GOTO :EOF

Here's a process using flag files that should allow you to do the copies in parallel.

First, set up a flag file in the temporary directory by selecting a name using the random-number generator, and create a file with suffix "a" as a place-keeper.

Next, start each of the processes you require to run in parallel but put the process into its own batch in th following format:

@ECHO OFF
SETLOCAL
ECHO robocopy %2 %3 %4 %5 %6 %7 %8 %9
PAUSE
DEL %1
EXIT

That is, first argument is the name of the flag file. In this case, you are executing robocopy with the remaining arguments. The pause is simply to simulate the robocopy operation.

The final operation before exiting is to delete the flag file.

Having submitted all of the jobs required, delete the "a" flagfile and execute the :wait loop until none of the flag files remains, which indicates each of the subsidiary batch files has ended.

Then run your renane procedure (but please change that name as rename is a batch keyword)

The timeout command prevents the batch becoming a processor-hog; it just waits for 1 second.

Another approach is to use the same wait loop, but wait for there to be no robocopy instances by executing tasklist and looking for a line beginning robocopy.exe. Wait until there are no such lines, then proceed.

Upvotes: 0

DavidPostill
DavidPostill

Reputation: 7921

Use start with the /w or start /wait option.

Start "" /w robocopy F:\DATA1 D:\Backup\  
Start "" /w robocopy G:\DATA2 D:\Backup\    
Start "" /w robocopy H:\DATA3 D:\Backup\    
Start "" D:\Backup\rename.bat

Now rename.bat will not run until the previous commands have completed.


Source Start a program, command or batch script (opens in a new window.)

Syntax

START "title" [/D path] [options] "command" [parameters]

Options:

/W or /WAIT Start application and wait for it to terminate. (for an internal cmd command or a batch file this runs CMD /K)

Upvotes: 1

Serenity
Serenity

Reputation: 1908

Stop using start. You use start to not make them wait. See start /?.

Upvotes: 1

Related Questions