我跑在Windows命令提示符运行以下命令
C:>tasklist /fi "Imagename eq BitTorrent.exe"
其中的输出是
Image Name PID Session Name Session # Mem Usage
================== ======== ================= =========== =========
BitTorrent.exe 6164 Console 3 24,144K
我需要提取只有一个字段,所述PID,即,从上述输出的数量6164。
如何实现这一目标? 更一般地,如何抽取一个子集从一个命令的窗口命令行上的输出中的字段的(1 /更多)?
类似于以前的答案,但使用特定的交换机在tasklist
跳过头和正确的行为,不论在空间形象的名字:
for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "imagename eq BitTorrent.exe" /fo csv') do @echo %~F
(如直接从CMD线运行,如果来自批次运行更换%F
与%%F
最简单的方法是使用WMIC:
c:\>wmic process where caption="BitTorrent.exe" get ProcessId
编辑:由于WMIC没有窗户的家庭版本的一部分:
for /f "tokens=1,2 delims= " %A in ('tasklist /fi ^"Imagename eq cmd.exe^" ^| find ^"cmd^"') do echo %B
这里使用caption.You的CMD可以在查找和任务列表参数改变它。 如果在批处理文件中使用,你需要%%B
和%%A
您可以使用WMIC命令不过滤的输出:
wmic process where name="BitTorrent.exe" get processid | MORE +1
更新:另一种方式:
@Echo OFF
FOR /F "tokens=2" %%# in ('tasklist /fi "Imagename eq winamp.exe" ^| MORE +3') do (Echo %%#)
Pause&Exit
PS:记住,你需要设置正确的令牌,如果应用程序的文件名包含空格。
文章来源: How to extract a specific field from output of tasklist on the windows command line