以字节为单位转换KB的wmic内存统计信息

时间:2013-10-23 09:27:46

标签: batch-file

使用下面的代码,我得到了以字节为单位的workSet.Size。

@echo off
setlocal enabledelayedexpansion

(For /F "delims=" %%A in ('"wmic process where Caption='Notepad.exe' get  ProcessId,WorkingSetSize /format:Texttable |findstr "[0-9P]" "') do (
set "line=%%A"
echo !line:~0,-1!
))>out.txt

我可以在上面的代码中以KB为单位获取此内存吗?

EDIT1:

我将代码修改为:

@echo off
setlocal enabledelayedexpansion
(For /F "delims=" %%A in ('"wmic process where Caption='Notepad.exe' get      ProcessId,WorkingSetSize /format:Texttable |findstr "[0-9P]" "') do (
set "mem=%%B"
set /a mem=mem/1024
echo %%A !mem! 
))>out.txt

并输出为:

ProcessId  WorkingSetSize  0 
5816       9142272         0 
5246       5673423         0 

但我不想要那些0,我只需要下面的内容:

ProcessId  WorkingSetSize  
5816       9142272        
5246       5673423         

EDIT2

如果我使用下面的代码:

@echo off
setlocal enabledelayedexpansion
(For /F "tokens=1,2" %%A in ('"wmic process where Caption='Notepad.exe' get   ProcessId,WorkingSetSize /format:Texttable |findstr "[0-9P]" "') do (
set "mem=%%B"
set /a mem=mem/1024
echo %%A !mem! 
))>new.txt

然后输出就像:

ProcessId 0 
5216      6112 
1152      6524 

1 个答案:

答案 0 :(得分:1)

如果数字总是小于2 GB,那么您可以使用此

@echo off
setlocal enabledelayedexpansion
(For /F "tokens=1,2" %%A in ('"wmic process where Caption='HsvDataSource.exe' get  ProcessId,WorkingSetSize /format:Texttable |findstr "[0-9]" "') do (
set "mem=%%B"
set /a mem=mem/1024
echo PID:%%A %%B=!mem! KB
))>out.txt

解决您的 EDIT2

@echo off
setlocal enabledelayedexpansion
>new.txt echo ProcessId  WorkingSetSize
(For /F "tokens=1,2" %%A in ('"wmic process where Caption='Notepad.exe' get   ProcessId,WorkingSetSize /format:Texttable |findstr "[0-9]" "') do (
set "mem=%%B"
set /a mem=mem/1024
echo %%A !mem! 
))>>new.txt