将特定流程的编号与编号进行比较 [英] Compare number of a specific process to a number

查看:39
本文介绍了将特定流程的编号与编号进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助此线程,可以获得特定进程的编号:

I get the number of a specific process with the help of this thread:

我希望将此命令的结果分配给一个变量,然后将该变量与数字进行比较.我的代码如下:

I hope to assign the result of this command to a variable, then compare the variable with a number. My code is listed as below:

@echo off
setlocal enabledelayedexpansion 
set procName=chrome.exe
set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"
if !a! equ 1 (
    echo !a!
    echo %procName% starts to run...
) else (
    echo !a!
    echo %procName% has not run!
)

我在这里得到"0"'set a = tasklist/FI"IMAGENAME eq%procName%" 2> NUL |找到/I/C%procName%"'命令.它还为我提供了"echo!a!"的"Echo Closed"提示.

Here I got '0' for 'set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"' command. It also gives me "Echo closed" hint for 'echo !a!'.

仅供参考,在cmd中运行以下命令时

FYI, when running the following command in cmd

任务列表/FI"IMAGENAME eq chrome.exe" 2> NUL |找/c/i"chrome.exe"

tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find /c /i "chrome.exe"

设置a =任务列表/FI"IMAGENAME eq chrome.exe" 2> NUL|找到/c/i"chrome.exe"

set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find /c /i "chrome.exe"

输出分别为16和0.

是什么原因?如何将命令的结果分配给变量?如何将变量与数字进行比较?

What's the reason? How could I assign the result of a command to a variable? How to compare the variable to a number?

非常感谢您!

推荐答案

好吧, set a = tasklist/FI"IMAGENAME eq chrome.exe" 2> nul |查找/c"chrome.exe" 也不适合我.很好,因为我不知道该如何工作.

Well, set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>nul | find /c "chrome.exe" does not work for me either. Which is good because I don't know how that was supposed to work.

我相信这样做会更快,因为它没有 FIND.EXE 以及编写,读取和删除 proc_temp 的开销.

I believe that this will be faster, because it doesn't have the overhead of FIND.EXE and writing, reading and deleting proc_temp.

set a=0
for /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq chrome.exe"') do set /a a=a+1
echo Total chrome.exe tasks running: %a%

编辑:我刚刚发现 set/a 不需要扩展变量,因此删除了 setlocal endlocal 命令并更改了 set/a 语法.

I just discovered that set /a does not require expanded variables and so removed the setlocal and endlocal commands and altered the set /a syntax.

这篇关于将特定流程的编号与编号进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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