How to get the current CPU usage and available memory in batch file?

后端 未结 4 1310
盖世英雄少女心
盖世英雄少女心 2021-02-06 04:17

I am creating a simple script that outputs the current user logged in, CPU usage for the current system and the available memory?

I have managed to get the current user/

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 04:42

    You could always utilise the systeminfo command, but then would be forced to go through a brief loading screen

    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%
    

    And that should do exactly what you want. This code can easily be modified to show Virtual Memory as well. (The use of set totalMem=%totalMem:,=% and set availableMem=%availableMem:,=% gets rid of commas in the variables.)

    Mona

提交回复
热议问题