14

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 ?

3 Answers 3

15

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

3
  • All 3 answers work for me, but i dont understand them :( Can someone explain their code fragment or point me to documentation for 'for /f' , 'tokens', 'delims' Nov 23, 2012 at 5:53
  • Run 'command' in parens, then for each line of it's output: tokenize (the line) with comma being delimiter and put a second token into %variable (%F) and then run echo with a given variable value (sans quotes, that's what tilde is for). For more general explanation on for syntax (including for /f) simply do help for at command line
    – wmz
    Nov 23, 2012 at 16:13
  • @ashishmakani here is the documentation: ss64.com/nt/for_f.html
    – Zeeshan
    Dec 29, 2017 at 4:15
10

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

7
  • 2
    i cant install anything on the machine & have to use the standard windows install. Nov 22, 2012 at 8:52
  • wmic is a standard command for windows.Except home editions...I'll add other approches in my answer anyway.
    – npocmaka
    Nov 22, 2012 at 8:54
  • PS: Remember you need to set right the tokens if the app filename contain spaces. Nov 22, 2012 at 9:47
  • @Elektro Hacker - yes .If you need another field you'll have to define more tokens and access the right one.tasklist -v has 9 fields so the maximum possible tokens are 9 and the last filed will be accessible with %%H if the first token is %%A
    – npocmaka
    Nov 22, 2012 at 10:01
  • 1
    @npocmaka, did you make a minor typo in your comment above ? For 9 fields, wont the 9th field be %%I & not %%H ? Nov 23, 2012 at 5:49
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.