10

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!

Sander_
  • 121
  • 1
  • 1
  • 5
  • Is it possible that there are fewer "standard.exe" processes then "basic.exe" ones? If so, what should be done in that case? – Andriy M Jul 01 '11 at 20:06
  • standard.exe can only be less than or equal to basic.exe (running basic.exe creates one process called standard.exe). Thus, indeed it is very well possible that there are fewer "standard.exe" processes than "basic.exe" ones, in which case all basic.exe processes need to be closed down and restarted. – Sander_ Jul 04 '11 at 07:53

3 Answers3

11

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 
) 
Maynza
  • 748
  • 5
  • 18
  • Generally, I think, the second piece of code is not equal to the first one, because it would count "myapp.exe" together with "notmyapp.exe". – Andriy M Jul 01 '11 at 20:25
  • Thanks Maynza, it seems to work indeed. However, I'm still not sure how to do the comparison between both number of processes? Any hints? Thanks – Sander_ Jul 04 '11 at 09:32
  • Well you could store one or both of the values in a environment variable and then compare them. ie: 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 a==b do ( stuff ) – Maynza Jul 05 '11 at 12:05
6

If you don't want to write a file, replace the tasklist and set var1 commands with

for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "standard.exe"') do set var1=%%a

same for the second ones.

for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "basic.exe"') do set var2=%%a
Alfred
  • 21,058
  • 61
  • 167
  • 249
Cuda
  • 61
  • 1
  • 2
  • I'm not an expert on batch files. @Maynza 's Solution works as long as I try to set a variable for the output. so the line gives correct return value pasted directly to the cmd... but in batch set x=... gives 0... However, using Win10 x64, this solution worked for me! – dba Mar 29 '17 at 14:14
2

There is probably a neater way to do it, but the following code seems to do the trick:

:begin
tasklist | find /I /C "standard.exe">D:\tmpfile1.txt
tasklist | find /I /C "basic.exe">D:\tmpfile2.txt
set /p var1= <D:\tmpfile1.txt
set /p var2= <D:\tmpfile2.txt
if %var1% LSS %var2% goto restart
if %var1% EQU %var2% goto wait

:wait
echo waiting..
ping -n 300 127.0.0.1 > nul
goto begin

:restart
echo error has occured, all processes will be restarted
taskkill /f /im standard.exe
taskkill /f /im basic.exe
ping -n 30 127.0.0.1 > nul

goto begin

Cheers!

Sander_
  • 121
  • 1
  • 1
  • 5