Skip to content

Instantly share code, notes, and snippets.

@StephaneTy-Pro
Created March 31, 2016 11:36
Show Gist options
  • Save StephaneTy-Pro/84c94e2bb02b427b12af739b8c084cf9 to your computer and use it in GitHub Desktop.
Save StephaneTy-Pro/84c94e2bb02b427b12af739b8c084cf9 to your computer and use it in GitHub Desktop.
Batch To See Which PID was launched
@echo off
set PROCESSNAME=notepad2.exe
::First save current pids with the wanted process name
setlocal EnableExtensions EnableDelayedExpansion
set "RETPIDS="
set "OLDPIDS=p"
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessId ^| findstr [0-9]') do (set "OLDPIDS=!OLDPIDS!%%ap")
SET OLDPIDS
::Spawn new process(es)
start %PROCESSNAME%
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessId ^| findstr [0-9]') do (
if "!OLDPIDS:p%%ap=zz!"=="%OLDPIDS%" (set "RETPIDS=/PID %%a !RETPIDS!")
)
@ECHO.NEw PID ARE
SET RETPIDS
CALL:sleep 5
SET RETPIDS
@ECHO DONE
GOTO :EOF
:sleep -– waits some seconds before returning
:: -- %~1 – in, number of seconds to wait
FOR /l %%a in (%~1,-1,1) do (ping -n 2 -w 1 127.0.0.1>NUL)
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment