Solved Filter & process output from wmic memorychip get capacity

Hewlett-packard / Pavilion hpe
December 1, 2014 at 09:50:39
Specs: Windows 7 Professional, 3.6GHz 16GB
Hello. I want to create a batch file that gets the amount of installed RAM. I am planning to use this:

wmic memorychip get capacity

It outputs something like this:

Capacity
8589934592
8589934592

How would I make a code that removes the "Capacity" and adds together the values that are returned so that the total capacity can be displayed by typing something like:

Echo %TotalCapacity%

If infinity is infinite, is the square root of infinity infinite as well?
- Kurp Von Steiner


See More: Filter & process output from wmic memorychip get capacity

Report •


#1
December 1, 2014 at 19:49:15
hypothetically, this would do it:
for /f %%a in ('wmic memorychip get capacity^|find /v "capacity"') do set /a memcap+=%%a

but for the limitations of batch. Anything over batch limits will have negative numbers. You will need another application to add the numbers in excess of batch integer limits (i forget what they are). To feed the other application, just put the lines out to a "temp" file:
@echo off>temp
for /f %%a in ('wmic memorychip get capacity^|find /v "capacity"') do >>temp echo %%a

Here's a complete solution, using vbscript to add the long integers:
::====== begin batchscript
@echo off
set k=
for /f %%a in ('wmic MEMORYCHIP get capacity^|find /v /i "capacity"') do call :xx %%a
echo STRING TO ADD: %k%0
pause
:: EDITED: removed path reference. substitute your own preference
for /f %%z in ('cscript /nologo calcit.vbs %k%0') do set memcap=%%z
echo total memory, stored in MEMCAP: %memcap%
goto :eof

:xx
echo adding element: %1
set k=%k%+%1
::========== end batchscript
(also, beware and watch out for unicode issues, since that's what WMIC puts out. Some things handle it, some don't.)

'----------------begin vbscript "CALCIT": a basic universal calculator
'Simple commandline calculator, but handy to have around (batch math is feeble), this is just a "front-end" to vbscript "eval".
'To use (example): cscript /nologo calcit.vbs ((172+6)*3) * 2^8/34 mod 4
'or another, demonstrating fractions: 12.7 / .22
'
'Also note it will do hex-to-decimal conversion and hex math, like: calcit &h70 + &hC, or &h70 + 12
'(but the results always rendered in decimal, alas.)
'Will also concatenate numbers using "&". Also supports interactive mode, which needs more work for "clear" and "store".
'--------- begin vbscript code "CALCIT.VBS"
set con=wscript.stdin
set cout=wscript.stdout
set t=wscript.arguments
if t.count=0 then
aa=0
cout.write ("enter the math: ")
'-- deprecated - flush this! do while len(aa)>0
do until con.atendofstream
aa=con.readline
bb=eval(bb & aa)
cout.writeline("cur.ttl: --------"&cstr(bb))
loop
else
dim a(99)
for i=0 to t.count-1
aa=aa&" "&t(i)
next
cout.writeline(eval(aa))
end if
'-------- end vbscript

message edited by nbrane


Report •

#2
December 2, 2014 at 01:08:14
Thank you for the swift reply.

I put the batch code in a separate file called RAMCHECK.bat, and put the vbs code into calcit.vbs

I call RAMCHECK from another script which then calls the main script again when done, however %memcap% displays "Input Error:"

Am I missing something?

If infinity is infinite, is the square root of infinity infinite as well?
- Kurp Von Steiner


Report •

#3
December 2, 2014 at 09:04:25
✔ Best Answer
Better idea is to just use a different counter.
for /f "tokens=2 delims==" %%a in ('wmic os get TotalVisibleMemorySize /format:value') do set mem=%%a
set mem

EDIT: And if you're going to use VBScript, use that WMI interface.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

Related Solutions

#4
December 2, 2014 at 19:30:16
Razor's answer takes precedence, IMO. But, to fix my mistake and to save face, I forgot to remove the path to the vbscript. this was wrong:
for /f %%z in ('cscript /nologo \vbs\calcit.vbs %k%0') do set memcap=%%z
should be:
for /f %%z in ('cscript /nologo calcit.vbs %k%0') do set memcap=%%z
(corrected in my previous post)

This, assuming calcit.vbs is in same subdirectory as your batch. There are a couple other options available as well. This batch simply creates a "bare-bones" vbs from scratch, so it should always be there:
@echo off
>temp.vbs echo wscript.echo eval(wscript.arguments(0))
set k=
for /f %%a in ('wmic MEMORYCHIP get capacity^|find /v /i "capacity"') do call :xx %%a
for /f %%z in ('cscript /nologo temp.vbs %k%0') do set memcap=%%z
echo total memory, stored in MEMCAP: %memcap%
goto :eof

:xx
echo adding element: %1
set k=%k%+%1
::===== end batch
Another option, just divide or truncate the values returned by WMIC by 3 digits (/1000) and report in K instead of bytes, with a slight loss in information resolution:
::======= begin script
@echo off
set memcap=0
for /f %%a in ('wmic MEMORYCHIP get capacity^|find /v /i "capacity"') do set /a memcap+=(%%a/1000)2>nul
set memcap=%memcap%000
echo total memory, stored in MEMCAP: %memcap%
::========= end batch
this value will be UNDER the actual, but by less than 2K.


Report •


Ask Question