0

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

Jamie
  • 101
  • 1
  • 10
  • Possible duplicate of [BATCH — Store command output to variable](https://stackoverflow.com/questions/50190167/batch-store-command-output-to-variable) – aschipfl Jul 11 '18 at 10:50
  • The comparison (`if %SYSTEM_RAM% > 100`, which should actually read `if %SYSTEM_RAM% gtr 100`) might deliver wrong results as soon as you leave the 32-bit signed integer range... – aschipfl Jul 11 '18 at 10:54

1 Answers1

1

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
)
: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.

  • If you wrap another `for /F` loop around the one you have, there was no need to leave the loop by `goto`; the "more than just one line"s result from bad Unicode-to-ASCII/ANSI conversion by `for /F`, but this can be fixed by a second `for /F`; your method could result in `system_ram` to contain a number followed by a carriage-return character... – aschipfl Jul 11 '18 at 10:52
  • @aschipfl: interesting, thanks. Feel free to edit my answer or to add a new one –  Jul 11 '18 at 10:53
  • I just found out that there is no CR character in the `system_ram` variable as the `wmic` return value is a number followed by spaces, which are default delimiters of `for /F` anyway. I was about to comment about handling of huge numbers for memory sizes (leaving the 32-bit signed integer range), then I found out `TotalVisualMemorySize` is returned in terms of Kilobytes, so there is still enough room luckily... ;-) – aschipfl Jul 12 '18 at 07:19