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

2
  • Possible duplicate of BATCH — Store command output to variable Commented Jul 11, 2018 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... Commented Jul 11, 2018 at 10:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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: interesting, thanks. Feel free to edit my answer or to add a new one
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... ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.