【问题标题】:Listening for applications opening and closing in batch监听应用程序的批量打开和关闭
【发布时间】:2018-01-14 03:46:45
【问题描述】:

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

@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. 光荣地自毁(又名exit

这样的监听是否可以批量进行,如果可以,怎么做?

(我也会对使用批处理以外的解决方案感到满意。)

【问题讨论】:

标签: windows batch-file


【解决方案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

【讨论】:

  • 这与我在斯蒂芬发表评论后实施的解决方案非常接近。我使用了for /f "usebackq" %%A in (`tasklist /fi "imagename eq Hearthstone.exe" ^| find /v /c "" `) do set shouldwait=%%A,然后使用了if /i "%shouldwait%" LEQ 1,但是您的解决方案更简洁,效果更好(我的方法遇到了一些问题)。
  • 谢谢,代码非常基本,如果需要,您应该能够轻松地调整它以满足您的需求。我试图使其尽可能简单且易于定制,希望它适用于您所做的任何事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 2011-05-19
  • 2018-11-02
  • 2019-05-29
  • 1970-01-01
相关资源
最近更新 更多