Skip navigation

JSI Tip 9766. How can I retrieve the amount of memory in a computer from the registry?

Using REG.EXE, built into Windows XP, Windows Server 2003, and later, or installed on Windows 2000 from the Support / Tools on the operating system CD-ROM, and VarLen.bat, I have scripted GetMem.bat to set an environment variable equal to a computers' physical memory, in megabytes.

The syntax for using GetMem.bat is:

GetMem MB \[Computer\]

Where:

MB       is a call directed numeric environment variable that will contain the amount of memory, in megabytes.

Computer is an optional parameter that contains a remote computer's NetBIOS name.
GetMem.bat contains:
@echo off
if \{%1\}==\{\} @echo Syntax GetMem MB \[Computer\]&goto :EOF
setlocal ENABLEDELAYEDEXPANSION
set comp=%2
if "%comp%" EQU "" set comp=%computerName%
set comp=%comp:"=%
set comp=\\%comp:\=%
set Key="%comp%\HKLM\HARDWARE\RESOURCEMAP\System Resources\Physical Memory"
:: Retrieve the binary string
For /f "Tokens=2,3" %%m in ('REG QUERY %Key% /V .Translated^|find "REG_RESOURCE_LIST"') do (
 set val=%%n
)
:: determine the length of the binary string.
call VarLen val Len
:: determine the position of the last 8 bytes of the binary string.
set /a pos=%len% - 8
set rev=!val:~%pos%!
:: Since the binary value stores the memory DWORD in reverse notation,
:: set the proper hexadecimal representation.
set hex=0x%rev:~6,1%%rev:~7,1%%rev:~4,1%%rev:~5,1%%rev:~2,1%%rev:~3,1%%rev:~0,1%%rev:~1,1%
:: Multiply each hexadecimal 2 character number by the proper bit shift.
set /a wk1=%hex:~0,4% * 16777216
set /a wk2=0x%hex:~4,2% * 65536
set /a wk3=0x%hex:~6,2% * 256
set /a wk4=0x%hex:~8,2%
:: Add each decimal value.
set /a wrk=%wk1% + %wk2% + %wk3% + %wk4% 
:: Convert to megabytes.
set /a mem=%wrk% / 1048576
:: Attempt to account for incorrect memory detection by making the results a multiple of 32MB.
set /a mem=%mem% + 31
set /a mem=%mem% / 32
set /a mem=%mem% * 32
endlocal&set /a %1=%mem%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish