Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all I want to write a simple script to get the memory available on a machine using the mem command...Problem is mem returns a few lines of info, what is the best way to parse this and get the number that I want after typing the mem command?
Any help appreciated,

Just a request for clarification: you've got Windows, with 1 GB memory. MEM is a legacy 16-bit command that runs under the Virtual DOS machine (NTVDM.) It only ever reports a maximum of 640 KB of conventional memory and 1 MB (not 1 GB) of extended memory. Is this really the value you want to obtain, or are you looking for the amount of free memory available to Windows?

More precisely mem reports up to 16 MB of extended and expanded memory, not just 1 MB. So stated, klint is right in pointing out mem is a legacy command bound to 16 bit memory addressing segmented architecture.

Hi thanks for the response - I really need to find out how
much memory is available on the machine to run an
application..Eg a script runs that works out that the machine
has a total of 1gb RAM available and then accordingly sets a
variable for the application to know to use half of the
memory available on the machine e.g 512mb....I hope this makes things clearer, any advice would be
appreciated

"available" as in free, or "available" as in total?
And "RAM" as in physical memory or "RAM" as in physical memory + page file?
EDIT: Also, how do you want this number? B? KB? MB?

Hi,
I am after the RAM that is available in total. RAM in terms of Physical Memory,
So my machine has 3GB of RAM when I look at the System properties,
I would like to see the information in MB ideally
Thanks,

So this is the type of problem VBScript with WMI excels at.
But it's not perfect: WMI needs to be running. This isn't normally a problem, but you never know how people modify their startup services. Also, this counts the total MB of each RAM module and adds them up, so it doesn't account for onboard devices stealing system memory. For instance, this PC has two 256MB chips in it, so the script reports 512MB; System Properties reports 504MB of RAM. Apparently, 8MB is reserved for the onboard video.
With that out of the way, this is a 6 line VBScript. If you're running it through cscript, it'll print the total MB of RAM. If not, it won't. Either way, the script will set the ERRORLEVEL to whatever it found. This allows you to do something like this from a command script:
totMem.vbs
SET /A halfMem=%ERRORLEVEL% / 2
yourApp -mem %halfMem%Why? Because I'm really just that nice and it was one extra line.
Script follows:
For Each o In GetObject("winmgmts:").InstancesOf("Win32_PhysicalMemory")
mem = mem + CDbl(o.Capacity) / 2 ^ 20
Next
On Error Resume Next
WScript.StdOut.WriteLine mem
WScript.Quit mem

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |