How to get the Percentage of memory utilization using WMIC in batch script?

T.Anand

I am not getting the result for Percentage of memory utilization in batch script using only W MIC. I am getting only total memory and free memory in bytes.

So how to a get those in Megabytes and how to calculate percentage of memory utilization?

Code :-

@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+=1
)

echo CPU Percentage = %Cpusage0%%%

set Times=0
for /f "skip=1" %%p in ('wmic ComputerSystem get TotalPhysicalMemory') do (
    set totalMem!Times!=%%p
    set /A Times+=1
)

set Times=0
for /f "skip=1" %%p in ('wmic OS get FreePhysicalMemory') do (
    set availableMem!Times!=%%p
    set /A Times+=1
)

set Times=0
for /f "skip=1" %%p in ('wmic OS get FreePhysicalMemory ^|findstr physical') do (
    set /a UsedMem= totalMem - availableMem
    set usedMem!Times!=%%p
    set /A Times+=1
)
set /a usedpercent=(usedMem*100)/totalMem

echo Free MEMORY = %availableMem0% Bytes
echo Total MEMORY = %totalMem0% Bytes
echo Used MEMORY = %UsedMem0% Bytes
echo Memory Utilization = %usedpercent0%%%

pause
Mofi

setlocal enabledelayedexpansion should be used only once in a batch file if there is no real necessity to use setlocal multiple times. This command does not just enable delayed expansion mode. It always copies also the entire current environment table (which can be up to 64 MB), the current states of command extension and delayed expansion, and the current directory path on stack (memory). The number of such environment pushes on stack is not unlimited. At least use endlocal between to avoid an early exit of batch processing because of a stack overflow. For more details see the answers on:

Even 64-bit Windows command interpreter (cmd.exe) uses 32-bit signed integers. Therefore the value range is limited to -2.147.483.648 to +2.147.483.647. In other words arithmetic operations with more than 2 GB can't be done without integer overflows producing wrong results.

Here is a commented batch file which does not work for all possible installed RAM configurations, but works for those which are typical in year 2016: 2 GB, 4 GB, 8 GB, 16 GB and 32 GB.

@echo off

rem Note: KB = KiB, MB = MiB and GB = GiB in this batch file, see
rem       https://en.wikipedia.org/wiki/Gibibyte for details on GiB.

rem Create a copy of current environment variables. Enabling additionally
rem delayed environment variable expansion is not required for this task.
setlocal

rem The command WMIC with the parameters CPU get loadpercentage outputs
rem one line per processor. The output of WMIC is in UTF-16 LE with BOM.
rem The output is redirected to a temporary file which is printed by
rem command TYPE to STDOUT which makes a better job on UNICODE to ASCII
rem conversion as command FOR. Note: 1 processor can have 1 or more cores.

set "CpuUsage=0"
set "Processors=0"
%SystemRoot%\System32\wbem\wmic.exe CPU get loadpercentage >"%TEMP%\cpu_usage.tmp"
for /F "skip=1" %%P in ('type "%TEMP%\cpu_usage.tmp"') do (
    set /A CpuUsage+=%%P
    set /A Processors+=1
)
del "%TEMP%\cpu_usage.tmp"

rem Calculate the CPU usage as percentage value of all processors.
set /A CpuUsage/=Processors
goto GetTotalMemory

rem Output of WMIC is in UTF-16 LE with BOM. The interpretation of this
rem output in ASCII/OEM can result in processing three lines instead of
rem just two with third line being just a carriage return. Therefore exit
rem each loop after assigning the value of second line to the variable.

:GetTotalMemory
for /F "skip=1" %%M in ('%SystemRoot%\System32\wbem\wmic.exe ComputerSystem get TotalPhysicalMemory') do set "TotalMemory=%%M" & goto GetAvailableMemory
:GetAvailableMemory
for /F "skip=1" %%M in ('%SystemRoot%\System32\wbem\wmic.exe OS get FreePhysicalMemory') do set "AvailableMemory=%%M" & goto ProcessValues

rem Total physical memory is in bytes which can be greater 2^31 (= 2 GB)
rem Windows command processor performs arithmetic operations always with
rem 32-bit signed integer. Therefore more than 2 GB installed physical
rem memory exceeds the bit width of a 32-bit signed integer and arithmetic
rem calculations are wrong on more than 2 GB installed memory. To avoid
rem the integer overflow, the last 6 characters are removed from bytes
rem value and the remaining characters are divided by 1073 to get the
rem number of GB. This workaround works only for physical RAM being
rem an exact multiple of 1 GB, i.e. for 1 GB, 2 GB, 4 GB, 8 GB, ...

rem  1 GB =  1.073.741.824 bytes = 2^30
rem  2 GB =  2.147.483.648 bytes = 2^31
rem  4 GB =  4.294.967.296 bytes = 2^32
rem  8 GB =  8.589.934.592 bytes = 2^33
rem 16 GB = 17.179.869.184 bytes = 2^34
rem 32 GB = 34.359.738.368 bytes = 2^35

rem But there is one more problem at least on Windows XP x86. About 50 MB
rem of RAM is subtracted as used by Windows itself. This can be seen in
rem system settings when 1.95 GB is displayed although 2 GB is installed.
rem Therefore add 50 MB before dividing by 1073.

:ProcessValues
set "TotalMemory=%TotalMemory:~0,-6%"
set /A TotalMemory+=50
set /A TotalMemory/=1073

rem The total memory in GB must be multiplied by 1024 to get the
rem total physical memory in MB which is always small enough to
rem be calculated with a 32-bit signed integer.

set /A TotalMemory*=1024

rem The available memory is in KB and therefore there is
rem no problem with value range of 32-bit signed integer.

set /A AvailableMemory/=1024

rem So the used memory in MB can be determined easily.

set /A UsedMemory=TotalMemory - AvailableMemory

rem It is necessary to calculate the percentage value in MB instead of
rem KB to avoid a 32-bit signed integer overflow on 32 GB RAM and nearly
rem entire RAM is available because used is just a small amount of RAM.

set /A UsedPercent=(UsedMemory * 100) / TotalMemory

if "%Processors%" == "1" (
    set "ProcessorInfo="
) else (
    set "ProcessorInfo= of %Processors% processors"
)
echo CPU percentage: %CpuUsage% %%%ProcessorInfo%
echo Free memory:    %AvailableMemory% MB
echo Total memory:   %TotalMemory% MB
echo Used memory:    %UsedMemory% MB
echo Memory usage:   %UsedPercent% %%

rem Discard the current environment variable table and restore previous
rem environment variables. The states of command processor extension
rem (default: ON) and delayed expansion (default: OFF) as well as the
rem original current directory are restored by this command although
rem not modified at all by the commands above.
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • rem /?
  • setlocal /?
  • set /?
  • type /?
  • wmic /?
  • wmic CPU get /?
  • wmic OS get /?
  • wmic ComputerSystem get /?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Get Task Manager Network Utilization Percentage

From Dev

how to find the percentage of CPU, RAM And memory usage using Batch FIle

From Dev

Memory utilization in %(Percentage) in Sun Solaris?

From Dev

Using wmic to get workgroup in a batch file

From Dev

Batch for loop using wmic

From Dev

wmic uninstall using batch file

From Dev

How to get the percentage of memory usage of a process?

From Dev

Text garble in batch script for wmic command

From Dev

Batch Script: Merging the output of WMIC calls

From Dev

Batch Script: Merging the output of WMIC calls

From Dev

Issues Renaming PC with WMIC in batch script

From Dev

How get only modification year using batch script?

From Dev

Get product name from WMIC for a variable in batch

From Dev

How to get Memory Usage in a variable using shell script

From Dev

How to get process ID of a specific process using wmic and check on error?

From Dev

TensorFlow: how to log GPU memory (VRAM) utilization?

From Dev

How to run wmic command in a batch file

From Dev

How to treat the "No instance found" of WMIC command in batch?

From Dev

How to write a batch file to return a WMIC result

From Dev

Get image dimensions using batch script (ImageMagick)

From Dev

Get image dimensions using batch script (ImageMagick)

From Dev

How to get line height in percentage using jquery?

From Dev

How to get width of a div in percentage using jQuery?

From Dev

How to get line height in percentage using jquery?

From Dev

How to close a file using wmic

From Dev

How to get batch script to automatically run on startup?

From Dev

How to: get batch script to log multiple robocopies

From Dev

how to parse a csv file using batch script?

From Dev

How to find large factorials using a batch script

Related Related

  1. 1

    How to Get Task Manager Network Utilization Percentage

  2. 2

    how to find the percentage of CPU, RAM And memory usage using Batch FIle

  3. 3

    Memory utilization in %(Percentage) in Sun Solaris?

  4. 4

    Using wmic to get workgroup in a batch file

  5. 5

    Batch for loop using wmic

  6. 6

    wmic uninstall using batch file

  7. 7

    How to get the percentage of memory usage of a process?

  8. 8

    Text garble in batch script for wmic command

  9. 9

    Batch Script: Merging the output of WMIC calls

  10. 10

    Batch Script: Merging the output of WMIC calls

  11. 11

    Issues Renaming PC with WMIC in batch script

  12. 12

    How get only modification year using batch script?

  13. 13

    Get product name from WMIC for a variable in batch

  14. 14

    How to get Memory Usage in a variable using shell script

  15. 15

    How to get process ID of a specific process using wmic and check on error?

  16. 16

    TensorFlow: how to log GPU memory (VRAM) utilization?

  17. 17

    How to run wmic command in a batch file

  18. 18

    How to treat the "No instance found" of WMIC command in batch?

  19. 19

    How to write a batch file to return a WMIC result

  20. 20

    Get image dimensions using batch script (ImageMagick)

  21. 21

    Get image dimensions using batch script (ImageMagick)

  22. 22

    How to get line height in percentage using jquery?

  23. 23

    How to get width of a div in percentage using jQuery?

  24. 24

    How to get line height in percentage using jquery?

  25. 25

    How to close a file using wmic

  26. 26

    How to get batch script to automatically run on startup?

  27. 27

    How to: get batch script to log multiple robocopies

  28. 28

    how to parse a csv file using batch script?

  29. 29

    How to find large factorials using a batch script

HotTag

Archive