Re: Detecting Process ID of the current cmd.exe
Posted: 21 Dec 2014 22:53
The solution below uses the new wmiCollection "data generating" function of FindRepl.bat version 2.2 that gives the same results of WMIC command, but run faster and works for any user. In this method FindRepl program generate the command lines of active processes and search for the same parameters used in its own execution, so this solution would only fail if this same program is executed two (or more) times at the exact same time. This point would require that two instances of FindRepl.bat program keep running in the same time interval, that is about 150 milliseconds in my not-so-fast computer.
Antonio
Code: Select all
@echo off
setlocal
set "source=wmiCollection('Win32_Process','CommandLine','ParentProcessID')"
set "search=%source:(=\(%"
set "search=%search:)=\)%"
for /F "usebackq tokens=4 delims=," %%a in (`FindRepl "/S:%source%" =search /J`) do set "PID=%%~a" & goto continue
:continue
echo %PID%
Antonio