Exit status of tasklist in batch file?

user613114 picture user613114 · Feb 11, 2011 · Viewed 17.8k times · Source

I am executing following command in a label inside a batch file:

tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" 2>nul && echo errorl:%errorlevel%

%1 is process running and %2 is its PID. Even if process and its PID matches or doesnt matches, I m getting "errorl:1" in o/p.

I am not sure whats wrong here. Any idea?

Answer

Bill picture Bill · Mar 6, 2012

You could pipe tasklist through the find command and get an errorlevel off of it.

Example:

tasklist | find "firefox.exe"
echo Error level = %ERRORLEVEL%

REM If firefox is running, the errorlevel is set to 0
REM If firefox is not running, errorlevel is set to 1