1

I am trying to wait for the process to end, Can't use the "Start /W" because the process opens from another program.

So all in all, I need some kind of scanner to look for if the process in the WaitSession and then continue the code to the 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

Thanks

crysal
  • 97
  • 8

3 Answers3

2

This should do it:

@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

The idea here is you keep polling the active task list and when the target process is found, you delay for a few seconds and then try again.

Once it is not found in the task list, you jump to the KillSession steps.

Jason Faulkner
  • 6,378
  • 2
  • 28
  • 33
1

Use wmic Windows 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
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • I use "cd /D C:\Windows\System32" to get into the folder with the executables that run in CMD. It otherwise just stays in the %HomePath% where there are non of the executables to run CMD – crysal Mar 20 '15 at 20:47
  • There is `path` [system environment variable](http://ss64.com/nt/path.html). It contains a list of folders. When a command is issued at the CMD prompt, the operating system will first look for an executable file in the current folder; if not found it will scan `%PATH%` to find it. By default, `C:\Windows\System32` should be there! Try e.g. `where taskkill`. The `where` [command](http://ss64.com/nt/where.html) locates and displays files in a directory tree. By default, the search is done in the current directory and in the `PATH` variable. – JosefZ Mar 20 '15 at 21:06
0

A 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

Use Win32_ProcessTraceStop to only catch terminations.

Serenity
  • 24
  • 2