有没有办法删除具有相同进程名称的条目?

Is there any way to remove the entries with same process names?

我正在尝试获取前 10 个内存消耗进程,我正在使用此 Question 中作为答案提供的代码。它工作正常,只是我不希望多个进程条目占用不同的内存大小,而且我无法对其进行排序,因为它的内存消耗不同。 代码是:

@echo off
setlocal EnableDelayedExpansion

(for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
set "size=         %%b"
echo !size:~-10!:%%a
)) > wmicc.txt
set i=0
for /F "skip=1 delims=" %%a in ('sort /R wmicc.txt') do (
echo %%a
set /A i+=1
if !i! equ 25 goto :end
)
:end

我可以得到如下输出:

96931840:iexplore.exe
82161664:explorer.exe
42319872:svchost.exe
31469568:dwm.exe
25690112:SearchIndexer.exe
17002496:taskhostex.exe
11007590:VCExpress.exe
8033894:avp.exe
7190528 :Skype.exe
7000416 :SkypeBrowserHost.exe

而不是现有代码的输出:

96931840:iexplore.exe
82161664:explorer.exe
42319872:svchost.exe
33656832:svchost.exe
31469568:dwm.exe
26943488:iexplore.exe
25690112:SearchIndexer.exe
18550784:svchost.exe
17002496:taskhostex.exe
16343040:svchost.exe
@echo off
setlocal EnableDelayedExpansion

for /f "delims==" %%a in ('set @ 2^>nul') do set "%%a="

(for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
set "size=         %%b"
echo !size:~-10!:%%a
)) > wmicc.txt
set i=0
for /F "skip=1 delims=" %%a in ('sort /R wmicc.txt') do (
 for /f "tokens=1*" %%p in ("%%a") do (
  rem the following line doesn't work
  rem if not defined "@%%q"
  rem the preceding line doesn't work
  SET "flag="
  FOR /f "tokens=1*delims==" %%v IN ('set @%%q 2^>nul') DO IF /i "%%v"=="@%%q" SET "flag=Y"
  IF NOT DEFINED flag (
   set "@%%q=Y"
   echo %%a
   set /A i+=1
   if !i! equ 25 goto :end
  )
 )
)
:end

这会将读入 %%p,q 的整行标记化,因此 %%q 包含进程名称。然后检查变量 @processname 是否已定义,如果未定义,则生成报告行并设置 @processname 以便第一次提及将是最后一次提及,因为该变量现已定义。

第一行旨在将任何现有 @* 变量设置为 empty;如果不存在 @* 变量并且插入符号转义 > 以告诉 cmd > 是要执行的命令的一部分,2^>nul 会抑制错误消息, 不是 for.

[稍后]

我发明的 if defined "varname" 似乎并没有真正起作用,尽管我测试它时它似乎起作用了。我已将该指令替换为有效的代码并将其标记出来。

@echo off
setlocal EnableDelayedExpansion

set num=25

set /A skip=-num
for /F "skip=1 tokens=1,2" %%a in ('wmic process get name^,workingsetsize') do (
   set "size=         %%b"
   set "size=!size:~-10!"
   if not defined proc[%%a] (
      set "proc[%%a]=!size!"
      set "size[!size!:%%a]=1"
      set /A skip+=1
   ) else if %%b gtr !proc[%%a]! (
      set "size[!proc[%%a]!:%%a]="
      set "proc[%%a]=!size!"
      set "size[!size!:%%a]=1"
   )
)

if %skip% lss 1 (set "skip=") else set "skip=skip=%skip%"
for /F "%skip% tokens=2 delims=[]" %%a in ('set size[') do echo %%a

输出示例:

   5517312:NisSrv.exe
   5537792:HD-LogRotatorService.exe
   5939200:spoolsv.exe
   6062080:WUDFHost.exe
   6168576:igfxpers.exe
   6279168:conhost.exe
   6369280:wmpnetwk.exe
   6533120:hkcmd.exe
   6692864:igfxtray.exe
   8073216:notepad.exe
   8077312:WMIC.exe
   8105984:taskhost.exe
   9306112:lsass.exe
  11157504:winlogon.exe
  11767808:taskhostex.exe
  13402112:SkyDrive.exe
  16629760:TabTip.exe
  17121280:SearchIndexer.exe
  26120192:HD-Agent.exe
  32325632:csrss.exe
  36429824:svchost.exe
  43114496:dwm.exe
 101998592:explorer.exe
 110260224:MsMpEng.exe
 114425856:chrome.exe