通过批处理获取某程序的CPU及内存使用
代码如下:
文件已经上传
@echo off&Setlocal EnableDelayedExpansion
:start
cls
echo 请输入进程名,如 notepad.exe
echo 直接回车查看 explorer.exe
set /p use=
if "%use%"=="" set use=explorer.exe
for /f "skip=2 tokens=2 delims==" %%i in (
'wmic process where "name='%use%'" get KernelModeTime^,UserModeTime /value'
) do (
set "x=!x! %%i"
)
call :Div %x% 5
>nul pause&exit /b
:Div
::不支持超大数和浮点
Setlocal EnableDelayedExpansion
set "dnd=%~1"&set "dsr=%~2"&set "dec=%~3"
if %dsr% equ 0 echo 除数不能是0!&goto :EOF
set/a x=%dnd%/%dsr%
if %x% equ 0 (
if %dnd% lss 0 (if %dsr% gtr 0 set "x=-0") else if %dsr% lss 0 set "x=-0"
)
if not defined dec set "dec=8"
if %dec% equ 0 echo %x%&goto :EOF
for /l %%i in (1 1 %dec%) do (
set/a dnd=^(a=!dnd!%%%dsr%^)*10,b=dnd/%dsr%
set "y=!y!!b!"
)
echo CPU使用率:%x%.%y:-=%%%
Endlocal
goto :EOF