How to set available memory to a string/variable in a batch file?

I am trying to set a variable by the name of SYSTEM_RAM to equal the output of the command below.

systeminfo |find "Available Physical Memory" 

This will let me find the available ram, then display it to the viewer so they don't see the background work of the command. Also this will allow me to run math on the amount such as,

if %SYSTEM_RAM% > 100 then echo good to go

I want to check if %SYSTEM_RAM% is less than %RAM_AMOUNT% and if so then run code accordingly

1 answer

  • answered 2018-07-11 06:02 a_horse_with_no_name

    I am using the following code in my batch file:

    for /f "skip=1" %%p in ('wmic os get TotalVisibleMemorySize') do ( 
       set system_ram=%%p
       goto :end
    echo %system_ram%

    The goto :end inside the loop is necessary as wmic will return more than just one line.

    %system_ram% can then be compared like this:

    set RAM_AMOUNT=8388608
    if %free_memory% geq %ram_amount% echo This is enough

    You could also check against the free memory using FreePhysicalMemory instead of the total installed memory.