我希望我正在运行的 .bat 脚本仅在另一个进程(正在运行的 .exe)运行时运行。
我可以使用什么命令来让我的脚本定期(比如每 5 秒)检查另一个进程,并且在没有该进程时自行关闭?
我目前的脚本只是杀死一些干扰游戏的进程,并启动一些我用于热键等的其他脚本。我写的这个小脚本只是帮助在游戏启动/终止后启动和停止所有这些外围脚本
@echo off
mode con: cols=80 lines=41
taskkill /f /im bio4.exe
taskkill /f /im RTrainer.exe
taskkill /f /im RE4_Min_Max.exe
taskkill /f /im trainer-hotkeys.exe
start steam://rungameid/254700
start /d "D:\Windows_Games\Steam Games\common\Resident Evil 4\TRAINER" RTrainer.exe
start /d "C:\Users\James\Documents\My-SCRIPTS" RE4_Min_Max.exe
start /d "C:\Users\James\Documents\My-SCRIPTS" trainer-hotkeys.exe
echo (new ActiveXObject("WScript.Shell")).AppActivate("Resident Evil 4"); > focus.js
cscript //nologo focus.js
del focus.js
color A
cls
:BEGIN
echo.
echo.
echo.
echo.
echo.
echo.
echo Launching RE4UHD
echo.
echo Press any to interrupt........
timeout 5 >nul
cls
:END
color A
cls
echo.
echo press any to terminate RE4UHD and Scripts
for /f "delims=: tokens=*" %%A in ('findstr /b ::: "%~f0"') do @echo(%%A
pause >nul 2>&1
taskkill /f /im bio4.exe
taskkill /f /im RTrainer.exe
taskkill /f /im RE4_Min_Max.exe
taskkill /f /im trainer-hotkeys.exe
color E
cls
echo.
echo Game and Scripts successfully terminated
ping 45 >nul 2>&1
谢谢!
答案1
[测试.bat]
@echo off
REM Initializing commands
:loop
REM Repeated commands
ping -n 1 -w 5000 192.168.255.254 > nul
REM If available then replace previous line with
REM TIMEOUT /T 5 /NOBREAK
for /f %%a in ('tasklist ^| find "MyEXE.EXE"') do if "%%a"=="MyEXE.EXE" goto :loop
REM Finalizing commands