PDA

View Full Version : PID Existence And Title Checking


nchernoff
2008-06-09, 15:55
Migrated From JP Software Wiki

Description

These two aliases and two functions make it easy to check whether a PID exists, and if it does, what the window title is, if there is one.

PIDExistChk

The PIDExistChk alias tests for the existence of a PID, such as those returned by the _pid or _startpid internal variables.

Here is how the alias may look in an alias file.

pidexistchk= & ^
iff "%1" NE "" then & ^
set pidexistchkfile=%@unique[%temp] & ^
tasklist /o >! %pidexistchkfile & ^
for %pidexistchkline in (@%pidexistchkfile%) do (echos. & iff "%@word[0,%pidexistchkline]" eq "%1" then echo 1 & endiff) & ^
del /f /q %pidexistchkfile & ^
unset pidexistchkfile & ^
endiff

A useful, related function is PIDExist. It returns 1 if the PID exists or 0 if it does not.

PIDExist=%@len[%@execstr[pidexistchk %1]]

PIDTitleChk

The PIDTitleChk alias works similarly, but returns the window title, if any, associated with the PID.

Here is how the alias may look in an alias file.

pidtitlechk= & ^
iff "%1" NE "" then & ^
set pidtitlechkfile=%@unique[%temp] & ^
tasklist /o >! %pidtitlechkfile & ^
for %pidtitlechkline in (@%pidtitlechkfile%) do (echos. & iff "%@word[0,%pidtitlechkline]" eq "%1" then echo %@instr[24,,%pidtitlechkline] & endiff) & ^
del /f /q %pidtitlechkfile & ^
unset pidtitlechkfile & ^
endiff

A useful, related function is PIDTitle. It returns the window title if there is one for the PID.