简体   繁体   中英

Windows batch file - how to execute command after program close that's not started in the batch file

I'm having a hard time finding an answer to this question. I'm looking specifically to execute a command in a batch file only after a program is terminated - however, a program that wasn't launched by the batch file.

My problem is this - the program I am actually launching in the batch file I want to wait on is in turn launching another program, which in turn launches another. This probably doesn't make any sense - but it's because it's a game launcher. It is for Final Fantasy XIV. The normal program that is launched to start it is ffxivboot.exe, which in turn launches ffxivlauncher.exe. That is a login window, and once you login, it in turn launches ffxiv_dx11.exe. So while I originally wrote it to wait on ffxivboot.exe, that process doesn't stay running so I am unable to wait on it.

Here's my file (excluded paths for simplicity):

taskkill /im someprogram.exe
ffxivboot.exe
timeout /t 60 /nobreak

### ??? need to wait on ffxiv_dx11.exe to close before executing next command

someprogram.exe

I added a timer to wait so that it gives me plenty of time to login - because the ffxiv_dx11.exe process doesn't start until after logging in.

Is what I'm trying to do possible? It's hard to search for answers to this because I only get results regarding when you're waiting on a task to end that was started from the batch file. But like I said, that one launches another which in turn launches another - so the original process is no longer running.

Thanks for any help!

taskkill /im someprogram.exe
ffxivboot.exe
timeout /t 60 /nobreak

:repeat
::### ??? need to wait on ffxiv_dx11.exe to close before executing next command
tasklist /fi "imagename eq ffxiv_dx11.exe"|find /i "=========================" >nul 2>nul &&(
   w32tm /stripchart /computer:localhost /period:10 /dataonly /samples:2  1>nul
   goto :repeat
)

someprogram.exe

try this

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

This is a vbs file that needs to run elevated. It waits for any program to exit and checks if it's notepad and springs to life restarting it.

Type in a elevated command prompt taskkill /f /I'm wscript.exe to stop it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM