Count number processes executing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Count number processes executing

#1 Post by darioit » 05 Oct 2016 23:58

Hello everybody,
I run this command on windows machine

Code: Select all

TASKLIST /FI "IMAGENAME eq chrome.exe"

and the result is:
Nome immagine PID Nome sessione Sessione n. Utilizzo mem
========================= ======== ================ =========== ============
chrome.exe 3264 Console 1 131.588 K
chrome.exe 1260 Console 1 4.620 K
chrome.exe 4300 Console 1 126.040 K
chrome.exe 4408 Console 1 104.528 K
chrome.exe 4572 Console 1 40.600 K
chrome.exe 3560 Console 1 22.516 K
chrome.exe 4232 Console 1 65.984 K
chrome.exe 4580 Console 1 27.016 K
chrome.exe 3692 Console 1 19.216 K
chrome.exe 3348 Console 1 34.988 K
chrome.exe 4272 Console 1 36.396 K
chrome.exe 1412 Console 1 60.556 K
chrome.exe 5336 Console 1 67.228 K
chrome.exe 5096 Console 1 44.032 K
chrome.exe 736 Console 1 78.864 K
chrome.exe 2464 Console 1 102.268 K
chrome.exe 3100 Console 1 92.584 K


I like to count how many times Chrome.exe appears and I run this script:

Code: Select all

@echo on
setlocal enableextensions
set count=0
for %%x in (TASKLIST /FI "IMAGENAME eq chrome.exe") do set /a count+=1
echo %count%
endlocal
pause


but ths result is 3, why?

Thanks in advance

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Count number processes executing

#2 Post by aGerman » 06 Oct 2016 00:54

It's because you used a simple FOR instead of FOR /F. (And option /NH would remove the two head lines.)

Code: Select all

for /f %%x in ('TASKLIST /FI "IMAGENAME eq chrome.exe" /NH') do set /a count+=1

or

Code: Select all

for /f %%x in ('TASKLIST /FI "IMAGENAME eq chrome.exe" /NH ^| FIND /C /V ""') do set /a count=%%x

Both untestetd though ...

Steffen

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Count number processes executing

#3 Post by darioit » 06 Oct 2016 01:42

Thank you very much, the first works very well

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Count number processes executing

#4 Post by aGerman » 06 Oct 2016 09:41

The second does also work but because it counts also the blank line on the top you have to subtract one.

Code: Select all

for /f %%x in ('TASKLIST /FI "IMAGENAME eq chrome.exe" /NH ^| FIND /C /V ""') do set /a count=%%x-1

Squashman
Expert
Posts: 4473
Joined: 23 Dec 2011 13:59

Re: Count number processes executing

#5 Post by Squashman » 06 Oct 2016 11:03

aGerman wrote:The second does also work but because it counts also the blank line on the top you have to subtract one.

Code: Select all

for /f %%x in ('TASKLIST /FI "IMAGENAME eq chrome.exe" /NH ^| FIND /C /V ""') do set /a count=%%x-1

Use /FO csv and then you do not have to subtract.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Count number processes executing

#6 Post by aGerman » 06 Oct 2016 12:08

Interesting. I never noticed that there was a difference.

Squashman
Expert
Posts: 4473
Joined: 23 Dec 2011 13:59

Re: Count number processes executing

#7 Post by Squashman » 06 Oct 2016 12:42

aGerman wrote:Interesting. I never noticed that there was a difference.

Yes. And it is completely opposite using WMIC but we all know the caveats of using WMIC.

Running this command I get empty line, header and then data.

Code: Select all

wmic logicaldisk where drivetype=4 get name,providername /format:csv


But if I run it with /format:table I get a header and then data.

Why would we expect any consistency out of MS.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Count number processes executing

#8 Post by aGerman » 06 Oct 2016 13:09

Squashman wrote:Why would we expect any consistency out of MS.

MS is pretty consitent in not having any consistency :lol: Grin and bear it :wink:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Count number processes executing

#9 Post by foxidrive » 06 Oct 2016 17:08

darioit wrote:I like to count how many times Chrome.exe appears and I run this script:


This is another way of handling it. Several ways of determining what could be counted follow it, just coz it amused me to fiddle with irrelevant things. ;)

Code: Select all

@echo off
for /f "delims=" %%a in ('TASKLIST /FI "IMAGENAME eq chrome.exe"^|find /i /c ".exe" ') do set num=%%a


Code: Select all

@echo off
for /f "delims=" %%a in ('TASKLIST /FI "IMAGENAME eq chrome.exe"^|find /i /c "chrome.exe" ') do set num=%%a


Code: Select all

@echo off
for /f "delims=" %%a in ('TASKLIST /FI "IMAGENAME eq chrome.exe"^|find /i /c "console" ') do set num=%%a


Code: Select all

@echo off
for /f "delims=" %%a in ('TASKLIST /FI "IMAGENAME eq chrome.exe"^|find /i /c "K" ') do set num=%%a

Post Reply