1

I get the number of a specific process with the help of this thread:

How to count amount of processes with identical name currently running, using a batchfile

I hope to assign the result of this command to a variable, then compare the variable with a number. My code is listed as below:

@echo off
setlocal enabledelayedexpansion 
set procName=chrome.exe
set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"
if !a! equ 1 (
    echo !a!
    echo %procName% starts to run...
) else (
    echo !a!
    echo %procName% has not run!
)

Here I got '0' for 'set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"' command. It also gives me "Echo closed" hint for 'echo !a!'.

FYI, when running the following command in cmd

tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find /c /i "chrome.exe"

set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find /c /i "chrome.exe"

the output is 16 and 0 respectively.

What's the reason? How could I assign the result of a command to a variable? How to compare the variable to a number?

Thank you so much!

Community
  • 1
  • 1
dehiker
  • 454
  • 1
  • 8
  • 21

3 Answers3

1

Well, set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>nul | find /c "chrome.exe" does not work for me either. Which is good because I don't know how that was supposed to work.

I believe that this will be faster, because it doesn't have the overhead of FIND.EXE and writing, reading and deleting proc_temp.

set a=0
for /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq chrome.exe"') do set /a a=a+1
echo Total chrome.exe tasks running: %a%

EDIT: I just discovered that set /a does not require expanded variables and so removed the setlocal and endlocal commands and altered the set /a syntax.

James K
  • 4,005
  • 4
  • 20
  • 28
  • I found a small error in my code that would cause it to count one too many lines. I've edited my answer and corrected the problem. I changed `skip=2` to `skip=3`. I also remove some unnecessary code. – James K Sep 02 '12 at 04:29
0

after this line in environment a has the pid of the process sought

for /F "tokens=1,2,*" %%a in ('tasklist /fi "imagename eq %procName%"') do if !%%a!==!%procName%!  set a=%b
rene
  • 41,474
  • 78
  • 114
  • 152
0

I think I found a solution:

tasklist /fi "imagename eq %procName%" 2>nul | findstr /i %procName% | find /c /v "">proc_temp
set /p current_num= < proc_temp
echo !current_num!

Also I think the code can be simplified. Hope some of you can give brief version :)

dehiker
  • 454
  • 1
  • 8
  • 21
  • BTW, when you call another bat before you use the variable, you SHOULD use 'call' in front of the bat you want to call, or the variable will be disabled. See a successful example: tasklist /fi "imagename eq %procName%" 2>nul | findstr /i %procName% | find /c /v "">proc_temp call Other.bat set /p current_num= < proc_temp echo !current_num! And a failed one: tasklist /fi "imagename eq %procName%" 2>nul | findstr /i %procName% | find /c /v "">proc_temp Other.bat rem This would disable current_num set /p current_num= < proc_temp echo !current_num! – dehiker Sep 01 '12 at 05:01
  • By using both `findstr.exe` and `find.exe` you are needlessly searching for the same information twice. Also, `find /v` removes the lines that it finds, screwing up the count. To use your code here you should change the first line to read `tasklist /fi "imagename eq %procName%" 2>nul | find /c "%procName%" > proc_temp`. – James K Sep 01 '12 at 17:08