使用cmd标题仅从任务列表中获取PID [英] Get only PID from tasklist using cmd title

查看:729
本文介绍了使用cmd标题仅从任务列表中获取PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所需的输出:

1234

只是PID.没什么-没有其他字符,数字或符号.

Just the PID. Nothing else - no other characters, numbers, or symbols.

我正在尝试运行tasklist,因此它只给我一个已命名或已命名进程的PID.

I'm trying to run tasklist so it gives me only the PID of a named or titled process.

tasklist | findstr /i "cmd.exe"是我得到的最接近的,但是结果太冗长了.我只想要PID号.

tasklist | findstr /i "cmd.exe" is the closest I've gotten, but the result is too verbose. I just want the PID number.

给我链接的奖励点是任务列表过滤器运算符的含义的描述-"eq","ne"等,因为它们不在

Bonus points for linking me a description of what the tasklist filter operators mean - "eq", "ne", etc, since they aren't anywhere in the documentation.

推荐答案

tasklist的困难之处在于其默认输出格式.例如,当命令行:

The difficult thing with tasklist is its default output format. For example, when command line:

tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running"

被执行,我们得到:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
cmd.exe                      12740 Console                    1      3'328 K
cmd.exe                      11020 Console                    1      3'304 K

除非列宽是固定的(我不会依赖),否则提取PID并不是一件容易的事,因为图像名称中也可以包含 SPACE ,因此使用诸如定界符之类的内容就不会工作.
一种可能的方法是计算直到第一张 SPACE 的第二行中的=-符号数,因此我们知道要删除图像名称而要截断的字符数,但这需要某种循环(使用goto),因此性能可能会很差.

Unless the column widths are fixed, which I would not rely on, extracting the PID is not that trivial, because the image name could also have SPACEs in it, so using such as delimiters would not work.
A possible way was to count the number of =-signs in the second line up to the first SPACE, so we know the number of characters to truncate to have the image name removed, but this requires some kind of loop (using goto), so the performance might be quite bad.

但是,tasklist还有其他输出格式可用.命令行:

However, there are other output formats available for tasklist. The command line:

tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO CSV

结果如下:

"Image Name","PID","Session Name","Session#","Mem Usage"
"cmd.exe","12740","Console","1","3'328 K"
"cmd.exe","11020","Console","1","3'304 K"

现在很容易提取PID:

Now it is quite easy to extract the PID:

@echo off
for /F "delims=" %%R in ('
    tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO CSV /NH
') do (
    set "FLAG1=" & set "FLAG2="
    for %%C in (%%R) do (
        if defined FLAG1 (
            if not defined FLAG2 (
                echo %%~C
            )
            set "FLAG2=#"
        )
        set "FLAG1=#"
    )
)

以下命令行使用另一种输出格式:

Another output formats is used by the following command line:

tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO LIST

结果如下:

Image Name:   cmd.exe
PID:          12740
Session Name: Console
Session#:     1
Mem Usage:    3'328 K

Image Name:   cmd.exe
PID:          11020
Session Name: Console
Session#:     1
Mem Usage:    3'304 K

有了它,获得所需的输出甚至更加简单:

With this it is even simpler to get the desired output:

@echo off
for /F "tokens=2" %%K in ('
   tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO LIST ^| findstr /B "PID:"
') do (
   echo %%K
)

顺便说一下,对于过滤器选项/FI,有以下可用的运算符:

By the way, for the filter options /FI, there are the following operators available:

  • eq-等于;
  • ne-不等于;
  • gt-大于;
  • lt-小于;
  • ge-大于或等于;
  • le-小于或等于;
  • eq -- equal to;
  • ne -- not equal to;
  • gt -- greater than;
  • lt -- less than;
  • ge -- greater than or equal to;
  • le -- less than or equal to;

Microsoft文档以及帮助消息(tasklist /?)均未解释其含义,但我发现以下外部资源:

The Microsoft documentation as well as the help message (tasklist /?) do not explain their meaning, but I found the following external resources:

  • Managing Windows Programs from the Command Line: Tasklist
  • Microsoft DOS tasklist command

这篇关于使用cmd标题仅从任务列表中获取PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆