使用wmic命令查找前10个进程

时间:2014-10-21 13:27:43

标签: windows batch-file command-prompt wmic taskmanager

我知道我可以做wmic流程清单简介'显示进程列表。有没有办法只查看使用最多内存的前10个进程?

2 个答案:

答案 0 :(得分:3)

@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
)) > wmic.txt
set i=0
for /F "skip=1 delims=" %%a in ('sort /R wmic.txt') do (
   echo %%a
   set /A i+=1
   if !i! equ 10 goto :end
)
:end
del wmic.txt

输出示例:

  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

答案 1 :(得分:1)

在DosTips上,我发布了JSORT.BAT - a hybrid JScript/batch utility,它可以在给定的行位置从数字上对stdin输入进行排序,并将结果写入stdout。该实用程序是纯脚本,可​​以在XP之后的任何Windows机器上运行。

我决定扩展JSORT.BAT以支持两个新选项:/S n所以你可以跳过(保留)标题,/C n这样你就可以得到前n个记录。

直接使用wmic process list brief的输出变得非常简单。唯一的技巧是我必须计算WorkingSetSize列的行位置,因为它根据列出的文件名的最长长度而变化。

@echo off
setlocal

:: Get full proc list in unicode
wmic process list brief >procList.tmp

:: Convert unicode to ANSII
type procList.tmp >procList.tmp2

:: Read the header line
<procList.tmp2 set /p "header="

:: Identify position of WorkingSetSize column
for /f "delims=W" %%A in ("%header%") do echo %%A>skipSize.tmp
for %%A in (skipSize.tmp) do set /a pos=%%~zA-1

:: Print the header, followed by the top 10 sorted by WorkingSetSize
type procList.tmp2 | jsort2 /n /r /p %pos% /s 1 /c 10

:: Delete the temp files
del procList.tmp procList.tmp2 skipSize.tmp

这是撰写本答案时的JSORT.BAT代码。按照上面的JSORT.BAT链接获取最新版本。

@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment

::************ Documentation ***********
::JSORT.BAT version 2.1
:::
:::JSORT [/Option [Value]]...
:::
:::  Sort lines of text from stdin and write the result to stdout.
:::  JSORT uses an ascending, case sensitive text sort by default.
:::
:::  Options:
:::
:::    /I   - Ignore case
:::
:::    /C n - Number of sorted lines to print. Skipped lines are always printed
:::           and do not contribute to the count. Default is -1 (all lines).
:::
:::    /N   - Sort consecutive digits as numbers instead of text. The numbers
:::           may be embedded within alpha text. JSort supports numbers up to
:::           20 digits long.
:::
:::    /P n - Begin sorting at character position n. Lines that have fewer than
:::           n characters are treated as equivalent values, and collate before
:::           all other lines. The default value is 1 (first character).
:::
:::    /R   - Sort the lines in Reverse (descending) order.
:::
:::    /S n - Number of lines to skip - default is 0.
:::           Skipped lines are not sorted (remain in place)
:::
:::    /V   - Display the version of JSORT.BAT.
:::
:::    /?   - Display this help
:::
:::JSORT.BAT was written by Dave Benham and originally posted at
:::http://www.dostips.com/forum/viewtopic.php?f=3&t=5595
:::

::************ Batch portion ***********
@echo off
setlocal enableDelayedExpansion

:: Define options
set "options= /?: /i: /c:-1 /n: /p:1 /r: /s:0 /v:"

:: Set default option values
for %%O in (%options%) do for /f "tokens=1,* delims=:" %%A in ("%%O") do set "%%A=%%~B"

:: Get options
:loop
if not "%~1"=="" (
  set "test=!options:* %~1:=! "
  if "!test!"=="!options! " (
      >&2 echo Error: Invalid option %~1
      exit /b 1
  ) else if "!test:~0,1!"==" " (
      set "%~1=1"
  ) else (
      set "%~1=%~2"
      shift /1
  )
  shift /1
  goto :loop
)

:: Display help
if defined /? (
  for /f "delims=: tokens=*" %%A in ('findstr "^:::" "%~f0"') do echo(%%A
  exit /b 0
)

:: Display version
if defined /v (
  for /f "delims=: tokens=*" %%A in ('findstr /bc:"::JSORT.BAT version" "%~f0"') do echo %%A
  exit /b 0
)

:: Transform and validate options
set /a "case=0%/i%, num=0%/n%, pos=%/p%-1, order=1-2*0%/r%, 1/^!(0x80000000&pos)" 2>nul || (
  >&2 echo Error: Invalid /P value.
  exit /b 1
)

:: Perform the sort
cscript //E:JScript //nologo "%~f0" %case% %num% %pos% %order% %/s% %/c%
exit /b 0


************* JScript portion **********/
var array=new Array(),
    nocase =WScript.Arguments.Item(0),
    numeric=WScript.Arguments.Item(1),
    pos    =WScript.Arguments.Item(2),
    order  =WScript.Arguments.Item(3),
        skip   =WScript.Arguments.Item(4),
        count  =WScript.Arguments.Item(5);
while (!WScript.StdIn.AtEndOfStream) {
  if (skip > 0) {
    WScript.Echo(WScript.StdIn.ReadLine());
    skip-=1
  } else {
    var expanded="", num="", raw=WScript.StdIn.ReadLine(), upper=((nocase==1)?raw.toUpperCase():raw);
    for( var i=pos; i<raw.length; i++ ) {
      var c=upper.substr(i,1);
      if (numeric==1 && c>="0" && c<="9") {
        num+=c;
      } else {
        if (num != "") {
          num="00000000000000000000" + num;
          expanded+=num.substr(num.length-20);
          num="";
        }
        expanded+=c;
      }
    }
    if (num != "") {
      num="00000000000000000000" + num;
      expanded+=num.substr(num.length-20);
    }
    var obj={expanded:expanded, raw:raw};
    array.push(obj);
  }
}
if (count<0) count=array.length;
if (count>array.length) count=array.length;
array.sort(function(a,b){return order*((a.expanded>b.expanded)-(a.expanded<b.expanded));});
for (var i=0; i<count; i++) WScript.Echo(array[i].raw);