Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.
September 21, 2005
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 offif {%1}=={} @echo Syntax GetMem MB [Computer]&goto :EOFsetlocal ENABLEDELAYEDEXPANSIONset comp=%2if "%comp%" EQU "" set comp=%computerName%set comp=%comp:"=%set comp=\%comp:=%set Key="%comp%HKLMHARDWARERESOURCEMAPSystem ResourcesPhysical Memory":: Retrieve the binary stringFor /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% - 8set 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% * 16777216set /a wk2=0x%hex:~4,2% * 65536set /a wk3=0x%hex:~6,2% * 256set /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% + 31set /a mem=%mem% / 32set /a mem=%mem% * 32endlocal&set /a %1=%mem%
You May Also Like