倾听批量打开和关闭的应用程序

时间:2018-01-13 12:39:45

标签: windows batch-file

我目前有一个小脚本可以同时打开两个程序:

@echo off

start "" "D:\...\Hearthstone Beta Launcher.exe"
start "" "C:\...\Innkeeper.exe"

exit

这有效,但现在我还想在关闭炉石时退出Innkeeper流程。

但是,还有一个额外的难点:Hearthstone Beta Launcher.exe实际上会打开第三个程序battle.net客户端,然后通过该程序打开Hearthstone.exe本身。

基本上,我想修改批处理脚本来执行以下操作:

  1. 开始Hearthstone Beta Launcher.exe
  2. 开始Innkeeper.exe
  3. 等待Hearthstone.exe打开
  4. 等待Hearthstone.exe关闭
  5. 终止Innkeeper.exebattle.net客户端
  6. 光荣地自毁(a.k.a。exit
  7. 这种倾听是否可以批量进行,如果可以,将如何进行?

    (我也很乐意使用除批次以外的其他解决方案。)

1 个答案:

答案 0 :(得分:1)

我找到并修改了一个批处理脚本,它应该能够很好地满足您的需求。它启动两个程序,等待它打开,等待它关闭,然后它终止程序并关闭。您可能希望将Hearthstone Beta Launcher.exe更改为没有空格。这是脚本。

@echo off
SETLOCAL EnableExtensions
color 07

set "EXE1=Hearthstone Beta Launcher.exe"
set "EXE2=Inkeeper.exe"
set "WAITEXE=Hearthstone.exe"
set "p1=Inkeeper.exe"
set "p2=battle.net"

rem ---- Opens Hearthstone and Innkeeper ----
start "" "%EXE1%" || echo Unable to start %EXE1%.
start "" "%EXE2%" || echo Unable to start %EXE2%.

rem ---- Waits for Hearthstone.exe to Open ----
:wait1
tasklist /FI "IMAGENAME eq %WAITEXE%" 2>NUL | find /I /N "%WAITEXE%">NUL
if "%ERRORLEVEL%"=="0" goto RUNNING1 & echo %EXE1% is running... else (
goto wait1
)

:RUNNING1

rem ---- Optional Running Code Here ----

rem ---- Waits for Hearthstone Beta Launcher.exe to Close ----
:wait2
tasklist /FI "IMAGENAME eq %WAITEXE%" 2>NUL | find /I /N "%WAITEXE%">NUL
if "%ERRORLEVEL%"=="0" goto wait2 else (
goto end
)

rem ---- Kills Program 1 and 2 ---- 

:end
cls
TASKKILL /F /IM %p1% /T || echo Unable to kill %p1%.
TASKKILL /F /IM %p2% /T || echo Unable to kill %p2%.
echo.
echo %p1% and %p2% have been terminated.
echo.
timeout 5
exit