批处理:批处理文件终止时关闭可执行文件

时间:2020-04-16 15:32:46

标签: batch-file

我有一个批处理文件,如果崩溃,它将继续打开游戏。我试图这样做,以便如果关闭批处理文件,程序将终止。

@echo off

if "%1" equ "loop" goto %1
start "" /WAIT /B "%~F0" loop 
Taskkill /f /im "Crossy Road.exe" >nul
goto :EOF

:loop
tasklist /FI "IMAGENAME eq Crossy Road.exe" 2>NUL | find /I /N "Crossy Road.exe">NUL
if "%ERRORLEVEL%"=="1" explorer.exe shell:appsFolder\Yodo1Ltd.CrossyRoad_s3s3f300emkze!App
goto loop
pause
exit

1 个答案:

答案 0 :(得分:0)

您可以在监视cmd.exe进程的批处理脚本中创建配套的vb,并调用另一个批处理脚本以结束所需的进程。 (该同伴批处理脚本也可以在您的主批处理程序中创建)


@ECHO OFF

::: Creates a VBS script to monitor the Cmd Process
:CreateMonitor
(
ECHO Set objWMIService = GetObject ("winmgmts:"^) 'Defines winmgmts service
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^) 'Gets Current Process Count
ECHO DO while proc.count ^> 0 'Establishes Loop that ends when no cmd.exe windows or bat programs are open
ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^) 'Gets Current Process Count
ECHO if proc.count ^< 1 then exit do 'exits while loop once bat program is closed
ECHO wscript.sleep 2500 'Delays loop by 2.5 seconds to reduce CPU usage
ECHO loop 'Returns to get new process Count
ECHO Set WshShell=createobject("wscript.shell"^) 'Creates Shell to
ECHO WshShell.run "INSERT PATH TO TASK-KILL .BAT HERE", 0, false 'Launches hidden .bat to close desired programs
) >"%TEMP%\myprogramMonitor.vbs"

::: Starts the VBS to monitor if cmd.exe is open

Start "" "%TEMP%\myprogramMonitor.vbs"

GOTO :EOF

在随同批次中为WScript进程添加Taskill也是谨慎的做法:

taskkill /pid WScript.exe /f /t >nul