I am trying to get windows processes matching some certain criteria, e.g. they are like "123456.exe" and trying to kill them with tasklist. I am trying to do it like that:
FOR /F "usebackq tokens=2 skip=2" %i IN (`tasklist |findstr /r "[0-9].exe") DO taskkill /PID %i
which is not right and I don't know why.... Can anyone give me a hint?
Thanx in advance!
FOR /F "usebackq tokens=2" %i IN (`tasklist ^| findstr /r /b "[0-9][0-9]*[.]exe"`) DO taskkill /pid %i
Several changes:
The command_to_process needs back quotes (``) on both sides of the command.
Pipes ("|") inside of the command_to_process need to be escaped with a caret ("^").
Your findstr command would match all processes that have a digit before the ".exe". For example, "myapp4.exe" would also have been killed. The version I provide will match process names solely containing numbers.
The "skip=2" option would skip the first two lines output from findstr, not tasklist. Since the regular expression won't match anything in the first two lines output from tasklist, you're safe to remove the skip option.
By the way, if you place this command in a batch script, remember to use "%%i" instead of "%i" for your parameters, or you'll get an error message like i was unexpected at this time.
FOR /F documentation
Findstr documentation
If the processes name difference is not very complex, e.g. if the name is always the same
you can use the /FI option of taskkill directly
taskkill /FI "IMAGENAME eq your_image_name_here.exe"
==> taskkill documentation
I used this in command line:
name variable can contains blank surround with "
SET name="process name you want to kill" && FOR /F "tokens=2,* delims=," %f IN ('TASKLIST /fo csv /v ^| FINDSTR /i /c:!name!') DO #TASKKILL /f /t /pid %f
Related
FOR /F "tokens=3 delims= " %i IN (query session | FINDSTR /i "console") DO set "ID=%i"
I am getting an error | was unexpected at this time.
Two very simple mistakes to avoid.
First by reading For /? use 'single quotes' for commands.
Secondly ^escape the |
FOR /F "tokens=3 delims= " %i IN ('query session ^| FINDSTR /i "console"') DO set "ID=%i"
Reminder as mentioned by #aschipfl in comment use doubled %% for both of your i's in a batch file or file.cmd
You should always read the usage information for a command utility before you use it. Had you done so, you would have noted that as you are specifically trying to isolate a line with the session name console, you could have used query session console instead of the less robust query session | FINDSTR /i "console". Of course, using the more appropriate command would mean that you do not have any issue with a horizontal bar, (pipe).
If you wanted, you could also skip the first, (header), line, and if you are certain, that your ID will always be the third whitespace separated token, you could then use:
From the Command Prompt, (cmd):
For /F "Skip=1 Tokens=3" %G In ('%SystemRoot%\System32\query.exe Session Console 2^>NUL') Do #Set "ID=%G"
Or from a batch file, (batch-file):
#For /F "Skip=1 Tokens=3" %%G In ('%SystemRoot%\System32\query.exe Session Console 2^>NUL') Do #Set "ID=%%G"
I am trying to kill specific services using the PID from "SC QUERYEX wuauserv". But I don't know how to pull the PID shown in the results to then run "taskkill /pid /f [PID]". I am trying to make a batch file which I can use on multiple machines remotely.
I have tried a couple of suggestions made in other similar questions found on google, but wuauserv is not being killed for some reason.
# echo off
cmd /c FOR /F "usebackq tokens=2 skip=3" %%i IN (tasklist /fi "services eq wuauserv") DO taskkill /PID %%
pause
The above is what I have, but it's not finding the specific service in the task list. Can anyone assist?
Essentially, because you specified usebackq you need to put your Command within backticks ie: IN (`Command`) DO () you also need to include the variable letter you specified earlier in the ending section which you have, right now it is just %% given you set up i as the variable letter it should be %%i.
Also the CMD /C portion is just not needed at all.
Thats said, just drop the UseBackQ it isn't necessary, here is yoru code cleaned up a little.
#(
SETLOCAL EnableDelayedExpansion
echo off
)
FOR /F "Tokens=2" %%I IN ('
Tasklist /fi "Services eq wuauserv"
^| FIND /I "wuauserv"
') DO (
ECHO Killing PID %%I
Taskkill /PID %%I
)
PAUSE
I have a list of strings I'm iterating over, and I need to exclude certain ones and include others in the subsequent processing.
Something like this:
listmystuff | for /F "usebackq" %%D in (`findstr /r "something*" ^| findstr /v "but not thisstuff"`) do interestingthing
This is wrong. What does work is having one of the findstr's but not both.
What would be right?
You want a pipe |, not conditional command concatenation &&. The pipe character must be escaped.
You only want one set of single quotes around the entire commmand. (or backticks if using usebackq option)
You need to double the percents if used within a batch file.
Your initial FINDSTR needs a filespec to search (or else data piped into it)
for /f "options" %%D in ('findstr "something" fileSpec ^| findstr /v "butNotThis"') do myThing
Give this a run with your show databases command. This is the usual way to use a for command to parse data.
for /F "delims=" %%D in ('show databases ^| findstr /r "something*" ^| findstr /v /c:"but not thisstuff" ') do echo %%D
I'm writing a batch file to find out if certain programs are currently running. Here is my code so far:
for %%x in ("notepad++.exe" "eclipse.exe") do (
tasklist /FI "IMAGENAME eq %%x" | find /I /C %%x
)
What I would like to be able to do is store the result from the "tasklist" line (which should be a number that is greater than or equal to 0) in a variable.
I guess I am really asking: is it possible to capture this output; and, if so, how?
With FOR /F:
for %%x in ("notepad++.exe" "eclipse.exe") do (
for /f %%c in ('tasklist /FI "IMAGENAME eq %%x" ^| find /I /C %%x') do echo %%c
)
Put the command whose output you want to capture inside single quotes and escape any characters with special meaning in the shell, such as the pipe, with the shell escape character ^.
Don't forget to look at FOR /? for additional options when using FOR /F.
I ran the following command on the windows command prompt
C:>tasklist /fi "Imagename eq BitTorrent.exe"
The output of which is
Image Name PID Session Name Session # Mem Usage
================== ======== ================= =========== =========
BitTorrent.exe 6164 Console 3 24,144K
I need to extract only one field, the PID, i.e. the number 6164 from the above output.
How do I achieve this ?
More generally, how do I extract a subset(1/more) of the fields from the output of a command on the windows command line ?
Similar to previous answers, but uses specific switches in tasklist to skip header and behave correctly irrespective of spaces in image names:
for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "imagename eq BitTorrent.exe" /fo csv') do #echo %~F
(as run directly from cmd line, if run from batch replace %F with %%F
the easiest way is with using WMIC:
c:\>wmic process where caption="BitTorrent.exe" get ProcessId
EDIT: As the WMIC is not part of home editions of windows:
for /f "tokens=1,2 delims= " %A in ('tasklist /fi ^"Imagename eq cmd.exe^" ^| find ^"cmd^"') do echo %B
Here is used CMD of the caption.You can change it in the find and tasklist parameters.
If this used in batch file you'll need %%B and %%A
You can use wmic command to not filter the output:
wmic process where name="BitTorrent.exe" get processid | MORE +1
UPDATE: Another way:
#Echo OFF
FOR /F "tokens=2" %%# in ('tasklist /fi "Imagename eq winamp.exe" ^| MORE +3') do (Echo %%#)
Pause&Exit
PS: Remember you need to set right the tokens if the app filename contain spaces.