18

我有 4 个批处理文件。我想同时one.bat运行two.bat。这两个批处理文件完成后,three.bat应该four.bat同时运行,并行。我尝试了很多方法,但 mot 工作正常。

谁能帮我解决这个问题?

4

4 回答 4

18

使用我为并行执行 shell 进程提供的解决方案的简化版本很容易做到这一点。有关文件锁定如何工作的说明,请参阅该解决方案。

@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
于 2012-09-30T22:00:37.160 回答
6

我也有同样的困境。这是我解决这个问题的方法。我使用 Tasklist 命令来监控进程是否仍在运行:

:Loop
tasklist /fi "IMAGENAME eq <AAA>" /fi "Windowtitle eq <BBB>"|findstr /i /C:"<CCC>" >nul && (
timeout /t 3
GOTO :Loop
)
echo one.bat has stopped
pause

你需要调整

<AAA>, <BBB>, <CCC>

脚本中的值,以便它正确过滤您的流程。

希望有帮助。

于 2012-10-25T17:53:06.057 回答
3

创建启动 one.bat 和 two.bat 的 master.bat 文件。当 one.bat 和 two.bat 正确结束时,它们会回显到已完成的文件

if errorlevel 0 echo ok>c:\temp\OKONE
if errorlevel 0 echo ok>c:\temp\OKTWO

然后master.bat等待这两个文件的存在

del c:\temp\OKONE
del c:\temp\OKTWO
start one.bat
start two.bat
:waitloop
if not exist c:\temp\OKONE (
    sleep 5
    goto waitloop
    )
if not exist c:\temp\OKTWO (
    sleep 5
    goto waitloop
    )
start three.bat
start four.bat

另一种方法是尝试使用 /WAIT 标志

start /WAIT one.bat
start /WAIT two.bat

但是您无法控制错误。

这里有一些参考

http://malektips.com/xp_dos_0002.html

http://ss64.com/nt/sleep.html

http://ss64.com/nt/start.html

于 2012-09-25T12:19:30.933 回答
1

只是添加另一种方式,也许是最短的。

(one.cmd | two.cmd) && (three.cmd | four.cmd)

概念真的很简单。errorlevel并行启动 1 和 2 ,完成后0运行 3 和 4。

于 2020-07-22T19:42:20.593 回答