Exit task list in batch file?

I execute the following command in the label inside the batch file: tasklist.exe / FI "USERNAME eq% USERDOMAIN% \% USERNAME%" / FI "IMAGENAME eq% 1" / FI "PID eq% 2" 2> nul && & && & echo errorl :% errorlevel%

% 1 is the process, and% 2 is its PID. Even if the process and its PID are the same or not, I get "errorl: 1" in o / p.

I am not sure what is wrong here. Any idea?

+4
source share
4 answers

In my opinion, you cannot use the error level at all,
because the task list always returns 0, even if pid is not found.

I suppose you need to analyze the output of the task list.

@echo off setlocal enableDelayedExpansion set "cmd=tasklist.exe /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2"" for /F "delims=*" %%p in ('!cmd! ^| findstr "%2" ') do ( echo found %%p ) 
+6
source

You can drag the task list through the find command and get the error level from 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 
+9
source

% variables% expand to line execution, so% errorlevel% will expand to some old value. (The fact that the code after && executes in general is your key, which the 0 command returns)

The following options are possible:

  • Use %errorlevel% or more correct IF errorlevel 1 ... on the next line
  • Call setlocal ENABLEDELAYEDEXPANSION first and then use !errorlevel!

Edit: I think tasklist is a mistake and / or stupid when it comes to exit codes, I wrote code that doesn't use exit code at all:

 @echo off if "%~1"=="SOTEST" ( start calc ping -n 2 localhost >nul for /F "tokens=1,2 skip=3" %%A in ('tasklist /FI "IMAGENAME eq calc.exe"') do ( call "%~0" %%A %%B ) call "%~0" dummy.exe 666 goto :EOF ) goto main :IsTaskRunning setlocal ENABLEEXTENSIONS&set _r=0 >nul 2>&1 (for /F "tokens=1,2" %%A in ('tasklist /FO LIST %*') do ( if /I "%%~A"=="PID:" set _r=1 )) endlocal&set IsTaskRunning=%_r%&goto :EOF :main call :IsTaskRunning /FI "USERNAME eq %USERDOMAIN%\%USERNAME%" /FI "IMAGENAME eq %1" /FI "PID eq %2" if %IsTaskRunning% gtr 0 (echo.%1:%2 is running) else (echo.%1:%2 is NOT running) 

Run it as test.cmd SOTEST and it prints:

 calc.exe:4852 is running dummy.exe:666 is NOT running 
+1
source

A simple solution, given that

  1) you can't get an errorlevel from tasklist, and 2) you can't directly pipe it to a FIND 

Just write it to a file using output redirection, and use FIND to check the file. Each time this is done, it overwrites the previous iteration, so there is no need to even clean up the files. It's amazing how many bat / cmd file limitations can be overcome with a simple notepad file!

 :TOP rem swap rems from good to bad to test set findvar=goodfile.exe rem set findvar=badfile.exe set scratchfile=scratch.txt tasklist /fi "status eq running" /fi "imagename eq %findvar%">%scratchfile% type %scratchfile% pause echo Looking for %findvar% find "%findvar%" %scratchfile% echo Error level = %errorlevel% pause IF errorlevel 1 GOTO BAD IF errorlevel 0 GOTO GOOD GOTO OTHER :BAD echo Errrlevel 1 - task not found PAUSE GOTO TOP :GOOD echo Errrlevel 0 - task is running pause GOTO TOP :OTHER echo something else ????? you blew it somewhere! 
0
source

Source: https://habr.com/ru/post/1339363/


All Articles