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