Is there a way to run a .cmd file when a specific

2019-07-25 09:06发布

问题:

I want the program GOPHER.exe to close when I close Netflix. I already have it set so that when I open Netflix, GOPHER also opens. Right now the command in the .cmd file to close GOPHER is "TASKKILL /IM gopher.exe". I want it to run when I close Netflix.

回答1:

This monitors for programs exiting using WMI (MS's implementation of WBEM). Here it checks for Notepad exiting and restarts it.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_ProcessStopTrace")

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
    If lcase(objReceivedEvent.ProcessName) = lcase("Notepad.exe") then 
        Msgbox "Process exited with exit code " & objReceivedEvent.ExitStatus
        WshShell.Run "c:\Windows\notepad.exe", 1, false
    End If
Loop