如何获取批处理文件中的当前CPU使用率和可用内存?

时间:2013-11-26 04:15:16

标签: batch-file cpu-usage ram

我正在创建一个简单的脚本,输出当前登录的用户,当前系统的CPU使用率和可用内存?

我设法让当前用户登录,但是也可以获得cpu使用和内存吗?

到目前为止,这是我的代码。

@echo off

for /f "tokens=3 delims=\" %%i in ("%USERPROFILE%") do (set USER=%%i) 2>&1  
echo "Logged On User: %USER%"  
echo. 

pause

为了获得cpu用法,我尝试过这个命令,但它似乎没有用。

for /f "skip=1" %p in ('wmic cpu get loadpercentage') do 
echo %p%

4 个答案:

答案 0 :(得分:7)

首先,从批处理文件运行时,for循环变量需要两个百分比符号 - %%p

其次,您需要回复%%p,而不是%p%

for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do echo %%p

从命令行:

for /f "skip=1" %p in ('wmic cpu get loadpercentage') do echo %p

答案 1 :(得分:4)

您可以随时使用systeminfo命令,但随后会被迫通过简短的加载屏幕

set totalMem=
set availableMem=
set usedMem=
REM You need to make a loop
for /f "tokens=4" %%a in ('systeminfo ^| findstr Physical') do if defined totalMem (set availableMem=%%a) else (set totalMem=%%a)
set totalMem=%totalMem:,=%
set availableMem=%availableMem:,=%
set /a usedMem=totalMem-availableMem
Echo Total Memory: %totalMem%
Echo Used Memory: %usedMem%

这应该完全符合你的要求。可以轻松修改此代码以显示虚拟内存。 (使用set totalMem=%totalMem:,=%set availableMem=%availableMem:,=%可以删除变量中的逗号。)

莫纳

答案 2 :(得分:0)

typeperf "\processor(_Total)\% Processor Time"  -SC 1 -y

C:\>logman create counter CPU_Usage3 -c "\Processor(_Total)\% Processor Time" -f csv -o %temp%\cpu.csv
The command completed successfully.

C:\>logman start CPU_Usage3
The command completed successfully.

C:\>logman stop CPU_Usage3
The command completed successfully.

C:\>type %temp%\cpu*.csv
The system cannot find the file specified.

Typeperf不适用于WindowsXP主页,但logman需要管理员权限并创建临时文件。 TYPEPERFLOGMAN。对于内存检查这些计数器: http://ss64.com/nt/syntax-performance-counters.html

答案 3 :(得分:0)

@echo off

setlocal enabledelayedexpansion

set Times=0

for /f "skip=1" %%p in ('wmic cpu get loadpercentage') do (

set Cpusage!Times!=%%p

set /A Times=!Times! + 1

)

echo Percentage = %Cpusage0%

pause