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

时间:2017-11-09 07:54:45

标签: windows batch-file cmd wmic taskmanager

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

@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"

[后]

我发明的/home/[user]/conf/web/httpd.conf似乎并没有真正发挥作用,虽然它在我测试时似乎有效。我已经使用可以正常工作并将其标记出来的代码替换了该指令。