等待进程结束并继续 CMD/BATCH 中的代码

Wait for process to end and continue code in CMD/BATCH

我正在等待进程结束,无法使用 "Start /W",因为进程从另一个程序打开。

所以总而言之,我需要某种扫描器来查找 WaitSession 中的进程,然后继续代码到 KillSession

@echo off
color 02
cd /D C:\Windows\System32
timeout -t 1

***WaitSession***
(Wait for this process to end) MightyQuest.exe

***KillSession***
taskkill /f /im PublicLauncher.exe
taskkill /f /im AwesomiumProcess.exe

谢谢

一个vbscript

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

Do
    Set objReceivedEvent = objEvents.NextEvent
    msgbox objReceivedEvent.ProcessName
Loop

使用 Win32_ProcessTraceStop 只捕获终止符。

应该这样做:

@echo off
color 02
cd /D C:\Windows\System32
timeout -t 1

SET TempFile="%Temp%\%RANDOM%.txt"

:WaitSession
REM Fetch the current process list. Store in a temp file for easy searching.
TASKLIST > %TempFile%

REM Reset the process "flag" variable.
SET "Process="

REM Check for a process with the target name by searching the task list for the target process name.
REM If output is returned, it will be put into the Process "flag" variable.
FOR /F "usebackq tokens=1 delims=-" %%A IN (`FINDSTR /I "MightyQuest.exe" %TempFile%`) DO SET Process=%%A

REM If nothing was returned, it means the process isn't running.
IF "%Process%"=="" GOTO KillSession

ECHO Process is still running.

REM Wait and then try again.
TIMEOUT /T 20
GOTO WaitSession

:KillSession
taskkill /f /im PublicLauncher.exe
taskkill /f /im AwesomiumProcess.exe

REM Cleanup.
DEL %TempFile% > nul

这里的想法是你不断轮询活动任务列表,当找到目标进程时,你延迟几秒钟然后重试。

如果在任务列表中找不到,则跳转到 KillSession 步骤。

使用wmicWindows Management Instrumentation Command

@echo off
color 02
rem why? cd /D C:\Windows\System32
timeout -t 1

:wait
TIMEOUT -t 3 /NOBREAK>nul
rem ***WaitSession***
rem (Wait for this process to end) MightyQuest.exe
for /F "tokens=*" %%G in (
    'wmic process where "name='MightyQuest.exe'" get name'
) do if /i not "%%G"=="No Instance(s) Available."  goto :wait

rem ***KillSession***
taskkill /f /im PublicLauncher.exe
taskkill /f /im AwesomiumProcess.exe