【问题标题】:Is there any way to remove the entries with same process names?有没有办法删除具有相同进程名称的条目?
【发布时间】:2017-11-09 07:54:44
【问题描述】:

我正在尝试获取前 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

【问题讨论】:

    标签: windows batch-file cmd wmic taskmanager


    【解决方案1】:
    @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
    

    【讨论】:

      【解决方案2】:
      @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 以便第一次提及将是最后一次提及,因为现在定义了该变量。

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

      [稍后]

      我发明的if defined "varname" 似乎实际上不起作用,尽管在我测试它时它似乎起作用。我已经用有效的代码替换了该指令并将其标记出来。

      【讨论】:

      • @San852 你确定,你只是想忽略倍数而不是增加它们的大小?
      • @Stephan 是的,我确定。我对进程名称更感兴趣,增加的大小对我来说不是太重要。
      • 您在问题中发布的数据显示空格分隔数字和名称。您在图像中发布的数据显示冒号。如果您的数据中有冒号,如图所示,则将 for /f "tokens=1*" %%p 更改为 for /f "tokens=1*delims=:" %%p
      • @Magoo 是的,对不起,我在代码中显示了错误的输出(现已更正),我在这一行中添加了冒号,它在上面的代码中 .echo !size:~-10!:%%a 。不幸的是,我仍然收到多个带有冒号和不带冒号的条目,我遗漏了什么?
      • 已修复。哦 - 顺便说一句:最好复制并粘贴代码和数据。我相信在您报告的数据中,您应该在开头的短第一列上留有空格,而不是在冒号之前。
      猜你喜欢
      • 1970-01-01
      • 2020-09-09
      • 2021-11-12
      • 2022-01-15
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      相关资源
      最近更新 更多