Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to use a batch file to compare the number of processes named "standard.exe", that are running on my Windows 7 machine, with the number of processes named "basic.exe". If the amount of processes called "standard.exe" equals the amount of processes called "basic.exe" nothing should happen, if the numbers are unequal, basic.exe should be restarted.

Any ideas? Already found the following code to check whether a process is running, but now I would like to count the number of processes carrying the same name.

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running

Thanks in advance!

like image 308
Sander_ Avatar asked Jul 01 '11 15:07

Sander_


1 Answers

Using your example simply replace the /N in find with /C to return the count of processes.

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /C "myapp.exe"

Then you can just reduce it down to :

tasklist | find /I /C "myapp.exe"

Although as Andriy M points out it will match both myapp.exe and notmyapp.exe.

As for the second part of your question, simply do this:

set a=tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /C "myapp.exe" 
set b=tasklist /FI "IMAGENAME eq myapp2.exe" 2>NUL | find /I /C "myapp2.exe" 
if not a==b do ( 
    stuff 
) 
like image 190
Maynza Avatar answered Sep 22 '22 10:09

Maynza