2017-06-20 106 views
1

我发现很难在这里修改脚本,以满足我的要求: https://stackoverflow.com/a/12665498/4683898等待并行批处理脚本或命令行

@echo off 
setlocal 
set "lock=%temp%\wait%random%.lock" 

:: Launch one and two asynchronously, with stream 9 redirected to a lock file. 
:: The lock file will remain locked until the script ends. 
start "" cmd /c 9>"%lock%1" one.bat 
start "" cmd /c 9>"%lock%2" two.bat 

:Wait for both scripts to finish (wait until lock files are no longer locked) 
1>nul 2>nul ping /n 2 ::1 
for %%N in (1 2) do (
    (rem 
) 9>"%lock%%%N" || goto :Wait 
) 2>nul 

::delete the lock files 
del "%lock%*" 

:: Launch three and four asynchronously 
start "" cmd /c three.bat 
start "" cmd /c four.bat 

我使用批处理脚本,不执行进一步的批处理脚本,但只需执行cmd命令(运行一段时间)并行,并且与上面的脚本一起工作正常。

但是,我希望能够同时运行多个命令/脚本,即3,4,5(或任何我想要的)命令,并行运行,我们称之为x。

所以,我想运行CMD命令的x量(即在平行执行),等待他们终止(使用/ C),然后执行所述一堆CMD命令的x量,然后下一个束,等等,直到所有cmd命令已经执行。

我该如何修改该脚本? (我已经做了几次尝试,尽管重复错误“进程无法访问该文件,因为它正在被另一个进程使用”,在启动批处理脚本中;我推测这个文件是指锁文件)

感谢

编辑:

@echo off 
setlocal 
set "lock=%temp%\wait%random%.lock" 

call :a start cmd.exe /c somecommandA 1 
call :a start cmd.exe /c somecommandB 2 
call :wait 
call :a start cmd.exe /c somecommandC 1 
call :a start cmd.exe /c somecommandD 2 
call :a start cmd.exe /c somecommandE 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 

回答

1

你可以用这个脚本方便地调用文件:

@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简单REPL等待。无论何时调用所有要异步运行的文件,都需要调用wait函数。然后你可以调用更多的脚本。

第二个参数是锁定文件的编号。在所有使用它们的脚本都关闭之前,请确保没有重复的数字(即在下一个call :wait之前)。尽管你不会用完数字,但没有理由使用重复。

+0

谢谢。我得到同样的错误“进程无法访问文件,因为它正在被另一个进程使用。”在启动批处理脚本中。此外,不幸的是它不起作用 - 它只是打开命令提示符等待进一步的命令。请参考我上面原始帖子中的编辑,了解我目前的内容。任何其他想法? – 151SoBad

0

只是提供另一种方式:

@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! 

编辑的评论。 bunch是并行运行的命令数。

@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 

第一for只是让您的命令行号(DATA部分的开始)

+0

谢谢斯蒂芬。使用该代码,我可以问我可以在哪里放置10000+个命令的其余部分? (每个命令都不同于另一个,我只是希望能够并行处理它们的批处理(即2,3或4个)。 – 151SoBad

+0

只是在批处理文件本身的末尾堆叠了命令。当然,您也可以使用外部文件。 – Stephan

相关问题