可以通过值来排序wmic进程表(在命令提示符窗口内)吗? [英] Can a wmic processes table (within the command prompt window) be sorted by a value?
问题描述
我一直在探索各种选项,例如 / format
标志,但是看起来只能在XML或HTML输出中进行排序。我想在命令提示符下本身排序。看起来,即使 TaskList 也无法做到这一点(我宁愿使用
运行命令的示例是 wmic进程get name,processid,workingsetsize
。进程似乎按processid进行排序,但是通过名称或内存使用( workingsetsize
)更有意义(在我的使用案例中) p>
根据需要,下面是一个wmic流程表输出示例:
WorkingSetSize
系统空闲进程0 20480
系统4 765952
smss.exe 384 393216
csrss.exe 500 2850816
wininit.exe 596 774144
csrss。 exe 612 6230016
winlogon.exe 672 2023424
services.exe 696 7192576
lsass.exe 704 9814016
svchost.exe 820 5287936
svchost.exe 872 7454720
atiesrxx.exe 936 1028096
显然,进程列表可能会很长,
@ECHO OFF
SETLOCAL
FOR / fdelims ==%% i IN('set $ 2 ^> nul')DO SET%% i =
FOR / fdelims =%% i IN获取名称^,ProcessId ^,WorkingSetSize')DO(
IF DEFINED $ 0(
SET wss = %% i
CALL SET wss = 0000000000000000000 %% wss:〜60 %%
CALL SET wss = %% wss:= %%
CALL SET wss = $ %% wss:〜〜20 %%
CALL SET %% wss %% = %% i
) ELSE(SET $ 0 = %% i)
)
FOR / ftokens = 1 * delims ==%% i IN('set $')DO ECHO
这应该可以。
其关键点是: WMIC
命令在字段名称之间需要逗号。这些需要在中转义/ f
首先删除开始$的所有环境变量,然后处理 WMIC
的每一行。第一行(标题)保存在 $ 0
中,然后每行保存在 $ size
这里的诀窍在于,列表中的SIZE不仅通过空格填充来左对齐,因此,零字符串首先被加到workingsetsize列的内容前面,这可能实际上是从列62,但列61是空间。使用60,因为子串设施从列0而不是列1开始计数。
然后每个空格都替换为[nothing],去除尾部空格(也方便的是,从col 61开始的空格)
结果字符串的最后20个字符形成workingsetsize列的前导零填充版本。
将$添加到tha,并将结果变量设置为最后,列出 $
变量的内容会产生所需的顺序列表。
< 。
请注意,声明所描述的 WMIC
过程输出是无序的。仔细检查会发现它不是按照ProcessID的顺序 - 按字母顺序(因为该列也左对齐)或按字母顺序。
OK - 修订版本,应自动调整为进程名称列的宽度:
@ECHO OFF
SETLOCAL
FOR / fdelims ==%% i IN('set $ 2 ^> nul')DO SET%% i =
SET / A COUNT = 0
FOR / fdelims =%% i IN('wmic process get Name ^,ProcessId ^,WorkingSetSize')DO(
IF DEFINED $ 0(
SET wss = %% i
CALL %% lopcmd %%
CALL SET wss2 = %% wss2:= %%
SET / A COUNT + = 1
CALL SET wss = $ %% wss2:〜-20 %% %% COUNT %%
CALL SET %% wss %% = %% i
)ELSE(SET $ 0 = %% i& set / a wsscol = 0& CALL:findcol& SET $ 0 = %% I)
)
FOR / ftokens = 1 * delims ==%% i IN('set $')DO ECHO(%% j
GOTO:eof
:findcol
如果%$ 0:〜0,1%==WSET lopcmd = CALL SET wss2 = 0000000000000000000 %% wss:〜%wsscol %%%& GOTO:eof
SET $ 0 =%$ 0:〜1%
SET / a wsscol + = 1
GOTO findcol
没有实质性的变化 - 只需计算所需的宽度,在标题中定位W,并建立一个适当的命令来设置变量准备处理...并且必须使用 CALL %% var %%
方法...
COUNT来区分具有相同工作集的行。 Count
简单地扩展用于排序的变量名,并使名称唯一。
I've been exploring various options, such as the /format
flag, however it appears that sorting is only possible in XML or HTML output. I would like to sort within command prompt itself. It seems that even TaskList cannot do this (and I would prefer to use wmic since it seems to perform faster).
An example of a command to run would be wmic process get name,processid,workingsetsize
. Processes appear to be sorted by processid, but it would make much more sense (in my use case, at least) to sort by name or memory usage (workingsetsize
).
As requested, here's an example wmic process table output:
Name ProcessId WorkingSetSize
System Idle Process 0 20480
System 4 765952
smss.exe 384 393216
csrss.exe 500 2850816
wininit.exe 596 774144
csrss.exe 612 6230016
winlogon.exe 672 2023424
services.exe 696 7192576
lsass.exe 704 9814016
svchost.exe 820 5287936
svchost.exe 872 7454720
atiesrxx.exe 936 1028096
Obviously, process lists can get very long, so I've cut it off there.
@ECHO OFF
SETLOCAL
FOR /f "delims==" %%i IN ('set $ 2^>nul') DO SET "%%i="
FOR /f "delims=" %%i IN ('wmic process get Name^, ProcessId^, WorkingSetSize ') DO (
IF DEFINED $0 (
SET wss=%%i
CALL SET wss=0000000000000000000%%wss:~60%%
CALL SET wss=%%wss: =%%
CALL SET wss=$%%wss:~-20%%
CALL SET %%wss%%=%%i
) ELSE (SET $0=%%i)
)
FOR /f "tokens=1*delims==" %%i IN ('set $') DO ECHO(%%j
This should do your sort.
The sticking points are: the WMIC
command requires commas between fieldnames. These need to be escaped in the for/f
All environment variables starting "$" are first deleted, then each line of WMIC
is processed. The first line (the header) is saved in $0
then each line is saved in $size
The trick here is that SIZE in the listing is not only left-justified by space-padded, hence the string of zeroes is first prefixed to the contents of the workingsetsize column which probably actually begins in column 62 - but column 61 is a space. "60" is used since the substring facility counts from column 0, not column 1.
Then each space is replaced by [nothing], stripping out the trailing spaces (also conveniently, the space from col 61)
The last 20 characters from the resultant string form a leading-zero-filled version of the workingsetsize column.
prepend a $ to tha, and set the resultant variable to the contents of the line that generated it
Finally, listing the contents of the $
variables produces the required list in the required order.
Note that the claims that the WMIC
process output described is in order whatever is erroneous. Closer examination would reveal that it is not in order of ProcessID - either alphabetically (since that column is also left-justified) or alphabetically.
OK - revised version, should auto-adjust to width of process-name column:
@ECHO OFF
SETLOCAL
FOR /f "delims==" %%i IN ('set $ 2^>nul') DO SET "%%i="
SET /A COUNT=0
FOR /f "delims=" %%i IN ('wmic process get Name^, ProcessId^, WorkingSetSize ') DO (
IF DEFINED $0 (
SET wss=%%i
CALL %%lopcmd%%
CALL SET wss2=%%wss2: =%%
SET /A COUNT+=1
CALL SET wss=$%%wss2:~-20%%%%COUNT%%
CALL SET %%wss%%=%%i
) ELSE (SET $0=%%i&set/a wsscol=0&CALL :findcol&SET $0=%%i)
)
FOR /f "tokens=1*delims==" %%i IN ('set $') DO ECHO(%%j
GOTO :eof
:findcol
IF "%$0:~0,1%"=="W" SET lopcmd=CALL SET wss2=0000000000000000000%%wss:~%wsscol%%%&GOTO :eof
SET $0=%$0:~1%
SET /a wsscol+=1
GOTO findcol
No substantial changes - just calculate the width required by locating the "W" in the header and establishing an appropriate command to set the variable ready for processing...and which has to executed using the CALL %%var%%
method...
Minor edit: introducing COUNT to distinguish between lines with identical workingsetsizes. Count
is simply extends the variable-name used for sorting and makes the name unique.
这篇关于可以通过值来排序wmic进程表(在命令提示符窗口内)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!