Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

batch file - How to extract a specific field from output of tasklist on the windows command line

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...