Waiting for Parallel batch scripts or command lines

前端 未结 2 1655
伪装坚强ぢ
伪装坚强ぢ 2020-12-22 03:28

I am finding it difficult to modify the script here to suit my requirements: https://stackoverflow.com/a/12665498/4683898

@echo off
setlocal
set \"lock=%temp         


        
相关标签:
2条回答
  • 2020-12-22 03:53

    just to offer an alternative way:

    @ECHO off
    start "MyCommand-%~n0" cmd.exe /c ping localhost
    start "MyCommand-%~n0" cmd.exe /c ipconfig /all
    start "MyCommand-%~n0" cmd.exe /c sysinfo
    :loop
    tasklist /fi "windowtitle eq MyCommand-%~n0"  | find "===" >nul && goto :loop
    echo finished!
    

    Edit for your comment. bunch is the number of commands running in parallel.

    @ECHO off
    setlocal enabledelayedexpansion
    set bunch=3
    
    for /f "delims=:" %%a in ('findstr /n /b "REM ==" %~f0') do set /a datastart=%%a+1
    set count=0
    for /f "skip=%datastart% usebackq delims=" %%a in ("%~f0") do (
      set /a "count=(count+1) %% %bunch%"
      echo starting: %%a
      start "MyCommand-%~n0" cmd.exe /c %%a
      if !count!==0 echo waiting & call :loop
    )
    echo waiting & call :loop
    echo finished!
    goto :eof
    
    :loop
    tasklist /fi "windowtitle eq MyCommand-%~n0"  | find "===" >nul && goto :loop
    goto :eof
    
    REM == START DATA ==
    ping localhost
    ipconfig /all
    systeminfo
    tasklist
    echo hello
    schtasks /query
    wmic bios get /value
    timeout 10
    

    the first for just gets the line number where your commands are (start of DATA section)

    0 讨论(0)
  • 2020-12-22 04:08

    You can call the files easily with this script:

    @echo off
    setlocal
    set "lock=%temp%\wait%random%.lock"
    
    
    call :a one.bat 1
    call :a two.bat 2
    call :wait
    call :a three.bat 1
    call :a name.bat 2
    call :a gfwagwa.bat 3
    
    
    exit /b
    :a
    start "%~2" cmd /c 9>"%lock%%2" %1
    exit /b
    :wait
    1>nul 2>nul ping /n 2 ::1
    for %%N in (%lock%*) do (
      ( rem
      ) 9>"%%N" || goto :Wait
    ) 2>nul
    

    call :wait simply replaces the waiting. Whenever you have called all files that are to be run asynchronously, call the wait function. Then you can call more scripts.

    The second parameter is the number of the lock file. Make sure you don't have duplicate numbers before all those scripts using them are closed (i.e. before the next call :wait). Though you're not going to run out of numbers anyway, no reason to use duplicates.

    0 讨论(0)
提交回复
热议问题