问题
I have around 24 batch files which I have to run 3 at a time and after finishing first 3 then I to have run next 3 files and so on.
Suppose I have files like 1.bat,2.bat, 3.bat and so on I need them to run first 3 upon finishing first 3 files I need next 3 files to run and so on till all 24 files.
回答1:
start 1.bat
start 2.bat
start /wait 3.bat
start 4.bat
start 5.bat
start /wait 6.bat
And so on. This assumes that the batch-file with the /wait
switch is the one to take longest. If that is not possible you can use this script here:
@echo off
start bat1.bat
start bat2.bat
start bat3.bat
call waitForFinish
start bat4.bat
start bat5.bat
start bat6.bat
call waitForFinish
Goto:eof
:waitForFinish
set counter=0
for /f %%i in ('tasklist /NH /FI "Imagename eq cmd.exe') do set /a counter=counter+1
if %counter% GEQ 2 Goto waitForFinish
After starting 3 batch-files you call the "function" waitForFinish
. This checks whether it finds more than 1 running command-line-process (one is used for the running batch-file so it will always be present and one additional line is above the output) and counts up for each window it finds.
If that counter is greater than 2 it will do the same again and again up to the point where only the running batch-file window is the only cmd.exe process found. If that is the case, the script returns to the top part of the script to start the next three.
回答2:
This solution use the method described at this answer:
@echo off
setlocal EnableDelayedExpansion
for /L %%i in (1,3,22) do (
set /A N1=%%i, N2=%%i+1, N3=%%i+2
( start "" !N1!.bat & start "" !N2!.bat & start "" !N3!.bat ) | set /P "="
)
In this method the first thread enters into a wait state that don't consume CPU until the three started Batch files ends. It is also very easy to write (and looks good! ;)
回答3:
What about the following script to execute three batch files in parallel and wait for them to finish? Basically it lets each batch file redirect its STDERR output to a temporary file, which is write-locked as long as the batch file is still running. As soon as the batch file terminates, the temporary file is accessible for writing and therefore it can and will be deleted. So here is the code:
@echo off
rem /* Execute three batch files in parallel, redirect their STDERR output
rem into individual temporary files: */
start "" cmd /C 2^> "1.tmp" "1.bat"
start "" cmd /C 2^> "2.tmp" "2.bat"
start "" cmd /C 2^> "3.tmp" "3.bat"
rem // Call sub-routine containing a polling loop:
call :POLL "1.tmp" "2.tmp" "3.tmp"
exit /B
:POLL {val_temp_file}*
rem /* Establish polling loop that tries to delete the temporary files;
rem if a batch file is still running, the respective temporary file
rem is not yet accessible, so deletion fails: */
:LOOP
rem // Delay execution to give processor some time:
> nul timeout /T 1 /NOBREAK
rem /* `for /F` loop to capture the STDERR output of the `del` command;
rem remember that `del` does not set the `ErrorLevel` unfortunately: */
for /F "delims=" %%E in ('
rem/ Discard STDOUT output and redirect STDERR output adequately: ^& ^
2^>^&1 ^> nul ^(^
rem/ `if exist` even works for already opened/accessed files: ^& ^
for %%F in ^(%*^) do if exist "%%~F" del "%%~F"^
^)
') do (
rem // Loop iterates, so there is STDERR output, hence loop back:
goto :LOOP
)
exit /B
来源:https://stackoverflow.com/questions/41584281/how-to-run-three-batch-files-in-parallel-and-run-another-three-batch-files-after