user1387160
user1387160

Reputation: 33

How to count of cmd.exe process with a specific title?

dividing product proc commands

I'd like to count cmd.exe process with a title dividing product proc. I'm able to count cmd.exe like below:

for /f "tokens=1 delims=" %%# in ('qprocess^|find /i /c /n "cmd.exe"') do (
    set number=%%#
)

But this code counts every cmd.exe. Is there a way to count cmd.exe with its title?

Upvotes: 2

Views: 1425

Answers (1)

Mofi
Mofi

Reputation: 49127

The following batch file demonstrates how this task can be done with a batch file using the command tasklist (SS64 documentation) documented also by Microsoft on Windows Commands page containing a link to tasklist documentation.

@echo off
set "ProcessCount=0"
for /F "skip=3 delims=" %%I in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq dividing product proc" 2^>nul') do set /A ProcessCount+=1
echo Number of cmd.exe processes with window title "dividing product proc": %ProcessCount%
echo/
pause

The first three lines of output of TASKLIST are skipped by FOR because of skip=3. Run in a command prompt window tasklist /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq dividing product proc" to see why the first three lines of output of TASKLIST are not of interest for counting cmd.exe processes with window title dividing product proc. delims= is used to disable line splitting behavior by specifying an empty list of delimiters to process the lines a very little bit faster.

It is also possible to use TASKLIST option /NH (No Header) to get the list of processes output without a header as suggested by sst (thanks) which makes usage of skip=3 unnecessary.

@echo off
set "ProcessCount=0"
for /F "delims=" %%I in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq cmd.exe" /FI "WINDOWTITLE eq dividing product proc" /NH 2^>nul') do set /A ProcessCount+=1
echo Number of cmd.exe processes with window title "dividing product proc": %ProcessCount%
echo/
pause

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • pause /?
  • set /?
  • tasklist /?

Upvotes: 3

Related Questions