I would like to find if an application is open and get it's PID. For this, I used,
wmic process where name="notepad.exe" get ProcessId
If there are two, notepad.exe
s open, it shows 2 PID's. I want to taskkill
one if 2 are open. How would I approach doing this. How can I check if there are 2 of the same named processes(with specific names) open at a time? Is there a way I can store the tasklist
in a list?
I think this is what you want:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET /A I=0
SET PIDS=
FOR /F "tokens=*" %%G IN ('wmic process where name^="notepad.exe" get ProcessId ^| FINDSTR /v ProcessId ^| FINDSTR /r /v "^$"') DO (
SET /A I=I+1
SET PIDS=!PIDS!%%G,
SET PIDS=!PIDS: =!
)
SET PIDS=!PIDS:~1,-1!
ECHO Found %I% processes
ECHO PIDs: %PIDS%
It will output the process count and the PIDs as a comma-separated list. Here's the output if i run it against chrome.exe
:
C:\tmp>q46277950.bat
Found 12 processes
PIDs: 1696,5668,11644,4240,1624,4284,15076,15236,2816,11388,5652,5840