These are my .bat scripts.
for /f "tokens=1-3 delims==" %%a in ('wmic computersystem get totalphysicalmemory /Value') do set /a "mem=%%b/1024/1024"
echo %mem%
These are my .bat scripts.
for /f "tokens=1-3 delims==" %%a in ('wmic computersystem get totalphysicalmemory /Value') do set /a "mem=%%b/1024/1024"
echo %mem%
The question uses /1024/1024
so I assume that we want the memory size in mebibytes (= 2^20 bytes). If we want decimal megabytes then of course all we neeed to do is to discard the last 6 digits.
@echo off
setlocal
set "mem="
for /f "tokens=2 delims==" %%a in (
'wmic computersystem get totalphysicalmemory /value'
) do for /f "delims=" %%b in (
"%%~a"
) do if not defined mem set "mem=%%~b"
:: At this point %mem% is the memory size in bytes. In order to convert to
:: mebibytes we need to divide by 2^20. However, set /a cannot work with
:: numbers greater than 2^31-1; we first convert to decimal megabytes and then
:: multiply by 0.95346.
:: (This will underestimate the mebibytes a little, by about 0.025%.)
set "memMB=%mem:~0,-6%"
set /a "mem=((memMB-memMB/21) + (memMB-memMB/22))/2"
echo This computer has %mem% MiB RAM
tokens=2
because the result returned by wmic
looks like
TotalPhysicalMemory=25697566720
^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^
1st token 2nd token
First set
and then set /a
because for /f
will include a carriage-return in the token, and trying to use it directly with set /a
will generate an error.
set /a
works in 32-bit two's complement arithmentic, so if %mem%
is greater than 2147483647 we cannot simply compute %mem%
/ 1024 / 1024.