Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to detect installed ram (mb) and use this value in script?..

1,151 views
Skip to first unread message

alexsupra

unread,
Apr 25, 2010, 4:09:26 PM4/25/10
to
how to detect installed to system memory (mb) and use this value in
script for making various actions (e.g. for applying different
configuration presets)?

approximate logical structure of script:
1. detecting ram value (via wmic or smth else)
2. making actions depending upon ram value
2.1. if ram value less than 512mb than "regedit /s slowcfg.reg"
2.2. if ram value equals or more 512mb but less 1024mb than "regedit /
s normcfg.reg"
2.2. if ram value equals or more 1024mb than "regedit /s fastcfg.reg"

Matthias Tacke

unread,
Apr 25, 2010, 6:55:58 PM4/25/10
to

You are half way there ;-)
start with
wmic computersystem GET TotalPhysicalMemory
parse the output with for /f and decide with if-statements,
calculate the mb by dividing bytes two times through 1024 with set /a

For help on details see:
for /?
if /?
set /?

--
Regards
Matthias

John Gray

unread,
Apr 26, 2010, 12:28:52 PM4/26/10
to
It may be of no relevance, but Mark Russinovich's PSINFO utility
produces a line
Physical memory: nnnn MB

You can reduce the output down to two lines by using the program's
filter
psinfo physical
(the line preceding the "Physical memory" line is
System information for \\PCNAME:
)

Matthias Tacke

unread,
Apr 26, 2010, 12:53:53 PM4/26/10
to
Hi John,
I was on the way to recommend it, but Psinfo returned on my PC:
Physical memory: 0 MB

which is equipped with 6GB DDR3

I just verified with the very last version v1.75, but still the same
erroneous result.

--
Regards
Matthias

John Gray

unread,
Apr 26, 2010, 1:24:12 PM4/26/10
to

Ah - my XP box gives the correct 1024 MB result.
That's very annoying...

alexsupra

unread,
Apr 26, 2010, 2:18:16 PM4/26/10
to

thank you.
would you mind to explain how to "parse the output with for /f " to
extract the ram value to some variable?

foxidrive

unread,
Apr 26, 2010, 2:56:32 PM4/26/10
to
On Mon, 26 Apr 2010 11:18:16 -0700 (PDT), alexsupra <alex...@gmail.com>
wrote:

>> > how to detect installed to system memory (mb) and use this value in
>> > script for making various actions (e.g. for applying different
>> > configuration presets)?
>>
>> > approximate logical structure of script:
>> > 1. detecting ram value (via wmic or smth else)
>> > 2. making actions depending upon ram value
>> > 2.1. if ram value less than 512mb than "regedit /s slowcfg.reg"
>> > 2.2. if ram value equals or more 512mb but less 1024mb than "regedit /
>> > s normcfg.reg"
>> > 2.2. if ram value equals or more 1024mb than "regedit /s fastcfg.reg"
>>
>> You are half way there ;-)
>> start with
>>    wmic computersystem GET TotalPhysicalMemory
>> parse the output with for /f and decide with if-statements,
>> calculate the mb by dividing bytes two times through 1024 with set /a

>thank you.


>would you mind to explain how to "parse the output with for /f " to
>extract the ram value to some variable?

@echo off

for /f %%a in (
'wmic computersystem GET TotalPhysicalMemory'
) do set "mem=%%a"

set mem=%mem:~0,-6%

if %mem% LSS 512 set file=regedit /s slowcfg.reg
if %mem% GTR 500 if %mem% LSS 1030 set file=regedit /s normcfg.reg
if %mem% GTR 1000 set file=regedit /s fastcfg.reg

echo %file%

pause

--
Regards,
Mic

Timo Salmi

unread,
Apr 26, 2010, 3:16:43 PM4/26/10
to
http://www.netikka.net/tsneti/info/tscmd117.htm

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

foxidrive

unread,
Apr 26, 2010, 3:48:20 PM4/26/10
to
On Tue, 27 Apr 2010 04:56:32 +1000, foxidrive <got...@woohoo.invalid>
wrote:

>@echo off
>
>for /f %%a in (
>'wmic computersystem GET TotalPhysicalMemory'
>) do set "mem=%%a"
>
>set mem=%mem:~0,-6%
>
>if %mem% LSS 512 set file=regedit /s slowcfg.reg
>if %mem% GTR 500 if %mem% LSS 1030 set file=regedit /s normcfg.reg
>if %mem% GTR 1000 set file=regedit /s fastcfg.reg
>
>echo %file%
>
>pause


The numbers will have to be checked on machines with different ram as I
made ballpark guesses about the figures returned.


--
Regards,
Mic

alexsupra

unread,
Apr 26, 2010, 6:03:15 PM4/26/10
to

thank you, that works!
i just have tested on my machine with 2gb ddr ram (windows xp pro sp3)
and got the result 2146.

alexsupra

unread,
Apr 26, 2010, 6:48:02 PM4/26/10
to
On 26 апр, 23:48, foxidrive <got...@woohoo.invalid> wrote:

several experiments on different machines (windows xp pro sp3) showed
that wmi returnes 536 for 512mb, 1072 for 1024mb, 2146 for 2048mb.

Ted Davis

unread,
Apr 26, 2010, 8:47:37 PM4/26/10
to

I was going to suggest that, but there was a clue in the original post
that led me to think the scheme had to be portable, and current versions
of his tools pop up a dialog box and make a registry entry the first time
they are used on a machine. It can be faked by forcing the registry entry
before invoking the utility, but that doesn't feel right.

--
Ted Davis (tda...@mst.edu)

ten.n...@virgin.net

unread,
Apr 27, 2010, 10:04:34 AM4/27/10
to

What about using the registry and a simple calculation, it may not be the
most accurate method but should be enough to make an informed decision (and
it's portable)

::----- START -----
@ECHO OFF & SETLOCAL ENABLEDELAYEDEXPANSION
SET KEY="HKLM\HARDWARE\RESOURCEMAP\System Resources\Physical Memory"
FOR /F "TOKENS=3" %%# IN ('REG QUERY %KEY% /V .Translated^|FIND "REG_"')
DO (
SET Val=%%#)
SET "HS=%Val:~-8%"
SET/A "HP1=(0x%HS:~1,1% / 1048576) + (0x%HS:~0,1% / 65536)"
SET/A "HP2=(0x%HS:~3,1% / 4096) + (0x%HS:~2,1% / 256)"
SET/A "HP3=(0x%HS:~5,1% / 16) + (0x%HS:~4,1%)"
SET/A "HP4=(0x%HS:~7,1% * 16) + (0x%HS:~6,1% * 256)"
SET/A "MemMb=%HP1% + %HP2% + %HP3% + %HP4%"
ECHO= Installed RAM: %MemMb% Mb
>NUL PING -n 6 127.0.0.1
::------ END ------

Matthias Tacke

unread,
Apr 27, 2010, 10:09:54 AM4/27/10
to
Am 2010-04-27 00:48, schrieb alexsupra:
> several experiments on different machines (windows xp pro sp3) showed
> that wmi returnes 536 for 512mb, 1072 for 1024mb, 2146 for 2048mb.

HI Alex,
the problem is that batch integers are limited to 32bit accuracy incl.
sign giving a maximum of 2GB.
Since wmic output is in decimal bytes and the last 6 places a
truncated, the exact values of MB (1024*1024) and GB (1024*1024*1024)
can't be matched.

--
Regards
Matthias

ten.n...@virgin.net

unread,
Apr 27, 2010, 10:10:27 AM4/27/10
to

The ENABLEDELAYEDEXPANSION is not required.

Matthias Tacke

unread,
Apr 27, 2010, 10:16:24 AM4/27/10
to
Am 2010-04-26 21:16, schrieb Timo Salmi:
> http://www.netikka.net/tsneti/info/tscmd117.htm
>
> All the best, Timo
>
Hello Timo,
depending on the exact Windows Version the output of
systeminfo is localized - so the find in the batch has
to be adapted then.

My german Win7 returns:
Gesamter physikalischer Speicher: 6.134 MB

--
Regards
Matthias

alexsupra

unread,
Apr 27, 2010, 12:24:31 PM4/27/10
to

thank you for this interesting alternative but calculations dont seem
to be so simple at least from the first glance... and i think these
are very much operations including working with system registry. if it
really could work faster than the following code its possible for this
interesting alternative to have sense in some cases:

::


@echo off
for /f %%a in ('wmic computersystem GET TotalPhysicalMemory') do set
"mem=%%a"
set mem=%mem:~0,-6%

echo %mem%mb is installed
if %mem% LSS 536 echo slow machine
if %mem% GTR 536 if %mem% LSS 1072 echo normal machine
if %mem% GTR 1072 echo fast machine!
pause
::

ten.n...@virgin.net

unread,
Apr 27, 2010, 1:00:40 PM4/27/10
to
On Tue, 27 Apr 2010 09:24:31 -0700 (PDT), alexsupra wrote:

> thank you for this interesting alternative but calculations dont seem
> to be so simple at least from the first glance... and i think these
> are very much operations including working with system registry. if it
> really could work faster than the following code its possible for this
> interesting alternative to have sense in some cases:

I'm not sure that I understand.
I've already done the calculations; How much more simple can it get!
How fast does it need to be? It returns a result on my machines almost
instantaneously!

Not only have I never seen this method used, it will also work in more
situations, since WMIC is not as readily available.

alexsupra

unread,
Apr 27, 2010, 1:44:24 PM4/27/10
to

i tried these lines inside cmd-file (copy-paste) and got the message
"the syntax of command line is incorrect" with instant closing of cmd
window.

Timo Salmi

unread,
Apr 27, 2010, 1:57:13 PM4/27/10
to
On 27.04.2010 20:44 alexsupra wrote:

> On 27 ???, 21:00, ten.nig...@virgin.net wrote:

>> Not only have I never seen this method used, it will also work in more
>> situations, since WMIC is not as readily available.

> i tried these lines inside cmd-file (copy-paste) and got the message
> "the syntax of command line is incorrect" with instant closing of cmd
> window.

His solution works just fine. You probably got caught with the wraps.

Matthias Tacke

unread,
Apr 28, 2010, 7:53:24 AM4/28/10
to
Am 2010-04-27 16:04, schrieb ten.n...@virgin.net:
> What about using the registry and a simple calculation, it may not be the
> most accurate method but should be enough to make an informed decision
>
> (and it's portable)

Well not overall.
On my Win7-64bit the referenced Key has a different structure,
the last 8 places are all zero.

Otherwise a nice solution.


--
Regards
Matthias

0 new messages