-1

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    There are already solutions on SO. You only need to google for them. – jeb Jan 11 '17 at 07:02
  • You might be interested in this: [execute batch files in parallel and get exit code from each](http://stackoverflow.com/a/41051895)... – aschipfl Jan 11 '17 at 11:57
  • IMO, question should have been closed as "too broad" or "must include MCVE". – halfer Jan 13 '17 at 17:53

3 Answers3

4
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.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
  • I tried running the same code which you have given. The result was: first three batch files ran and got closed, but still the counter value is greater than 1 and loop was repeating continuously. Next three batch files were not running. please suggest me, – UmamaheswaraReddy Ambati Jan 11 '17 at 10:52
  • Instead of using `findstr` to filter, you should use the filter capabilities of `tasklist` (see its `/FI` option)... – aschipfl Jan 11 '17 at 11:27
  • @UmamaheswaraReddyAmbati It seems that there was an additional line that was causing the problem... I changed the command a bit after aschipfl's suggestions as well and changed the incorrect 0 to 2. – geisterfurz007 Jan 11 '17 at 12:38
  • `if counter GTR 2` is wrong, it must be `if %counter% GEQ 2`! – aschipfl Jan 14 '20 at 10:17
  • @aschipfl Thanks for the note; took a while to understand what I had written there; I agree with Aacini's version being a lot cleaner. Fixed the code. – geisterfurz007 Jan 14 '20 at 10:39
3

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! ;)

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99