Reputation: 11
I am new to batch scripting and trying to search and kill a list of processes and not sure how to proceed further after the second pipe
@echo off
wmic process where "Name like 'java%%.exe' " get Processid, Caption, Commandline | find "abc" |
Here I am using WMIC to list all the java processes and filtering them using find to get the java processes with a specific word in the Commandline, now I need to kill these processes after finding them. Can this be achieved by taskkill or using a for loop after writing this to a file? I do not want to use call Terminate to kill the processes. can someone help me with this. Thanks in advance.
Upvotes: 1
Views: 2248
Reputation: 101606
This first looks for processes by .exe name and then looks at the command line of those after having learned the process id:
@echo off
setlocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
set exe=winver.exe
set findcommand=foo bar
REM start example programs
start "" "%exe%" %findcommand%
start "" "%exe%" %findcommand% baz
ping -n 3 localhost>nul&rem SLEEP
for /F "tokens=2 delims==" %%A in ('wmic process where "Name like '%exe%'" get ProcessID /format:list') do for /F "tokens=1,* delims==" %%B in ('wmic process where "ProcessID like '%%A'" get commandLine /format:list') do for /F %%D in ("%%~B") do (
(echo.%%C|find /I "%findcommand%" >nul)&&call taskkill /PID %%A
)
I'm sure there are ways to break this if the command line contains something echo
does not like but such is life with batch files...
Upvotes: 1