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 ?
You can use wmic command to not filter the output:
UPDATE: Another way:
PS: Remember you need to set right the tokens if the app filename contain spaces.
Similar to previous answers, but uses specific switches in
tasklist
to skip header and behave correctly irrespective of spaces in image names:(as run directly from cmd line, if run from batch replace
%F
with%%F
the easiest way is with using WMIC:
EDIT: As the WMIC is not part of home editions of windows:
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