Ram memory Question? [ SOLVED ]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Ram memory Question? [ SOLVED ]

#1 Post by Dos_Probie » 23 Sep 2014 19:08

Need to convert this wmic batch script from Bytes to GigaBytes with the same variable any ideas?
Thanks for the help, DP :wink:

Code: Select all

@echo off&color 1f
FOR /F "tokens=2 delims='='" %%A in ('wmic memorychip Get capacity /value') Do (Set "Ram=%%A")
FOR /F "tokens=1 delims='|'" %%A in ("%Ram%") Do (Set "Ram=%%A")
echo Usable RAM Memory: %Ram%
pause>nul
Last edited by Dos_Probie on 24 Sep 2014 09:07, edited 1 time in total.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Ram memory Question?

#2 Post by ShadowThief » 23 Sep 2014 19:57

The biggest problem you're going to have is that batch can't process numbers larger than 2147483647, so unless your computer has less than 2 GB of RAM, you're going to have to calculate the length of the string containing the number, take the first digit, and extrapolate the size based on that (if your number has 10 digits and starts with a 4, you have 4 GB of RAM).

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Ram memory Question?

#3 Post by Compo » 23 Sep 2014 19:59

Do you know that WMI on MemoryChip is providing the sizes of each physical stick of RAM? That means your code is going to provide only the total of the last stick found, not the total at each stick.
To verify this you could extend the code slightly to read

Code: Select all

WMIc MemoryChip Get Capacity, DeviceLocator


Incidentally PowerShell has a built in capability of converting the returned result according to the abbreviation e.g.

Code: Select all

GWmi CIM_PhysicalMemory | % {($_.devicelocator) + " = " + ($_.capacity / 1MB) + "MB"}

Code: Select all

GWmi CIM_PhysicalMemory | % {($_.devicelocator) + " = " + ($_.capacity / 1GB) + "GB"}

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Ram memory Question?

#4 Post by foxidrive » 23 Sep 2014 21:10

Compo wrote:Incidentally PowerShell has a built in capability of converting the returned result according to the abbreviation e.g.

Code: Select all

GWmi CIM_PhysicalMemory | % {($_.devicelocator) + " = " + ($_.capacity / 1MB) + "MB"}

Code: Select all

GWmi CIM_PhysicalMemory | % {($_.devicelocator) + " = " + ($_.capacity / 1GB) + "GB"}


Have I launched this incorrectly or is there an issue here:

Code: Select all

@echo off

powershell "GWmi CIM_PhysicalMemory | %% {($_.devicelocator) + " = " + ($_.capacity / 1MB) + "MB"}"
powershell "GWmi CIM_PhysicalMemory | %% {($_.devicelocator) + " = " + ($_.capacity / 1GB) + "GB"}"

pause



At line:1 char:50
+ GWmi CIM_PhysicalMemory | % {($_.devicelocator) + = + ($_.capacity / 1GB) + GB ...
+ ~
You must provide a value expression following the '+' operator.
At line:1 char:78
+ GWmi CIM_PhysicalMemory | % {($_.devicelocator) + = + ($_.capacity / 1GB) + GB ...
+ ~
You must provide a value expression following the '+' operator.
At line:1 char:79
+ GWmi CIM_PhysicalMemory | % {($_.devicelocator) + = + ($_.capacity / 1GB) + GB ...
+ ~~
Unexpected token 'GB' in expression or statement.
At line:1 char:30
+ GWmi CIM_PhysicalMemory | % {($_.devicelocator) + = + ($_.capacity / 1GB) + GB ...
+ ~~~~~~~~~~~~~~~~~~~~
The assignment expression is not valid. The input to an assignment operator must be an object that
is able to accept assignments, such as a variable or a property.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedValueExpression

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Ram memory Question?

#5 Post by Compo » 24 Sep 2014 03:09

foxidrive wrote:Have I launched this incorrectly or is there an issue here:
From your batch file try this:

Code: Select all

@echo off

PowerShell "& GWmi CIM_PhysicalMemory | %% {($_.devicelocator) + ' = ' + ($_.capacity / 1MB) + 'MB'}"
PowerShell "& GWmi CIM_PhysicalMemory | %% {($_.devicelocator) + ' = ' + ($_.capacity / 1GB) + 'GB'}"

pause

From the CMD Console try this:

Code: Select all

PowerShell "& GWmi CIM_PhysicalMemory | % {($_.devicelocator) + ' = ' + ($_.capacity / 1GB) + 'GB'}"

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Ram memory Question?

#6 Post by penpen » 24 Sep 2014 04:04

@Compo
Although your powershell call (using 'powershell "& ..." ') is working, there could be some commands that produce errors (actually i don't remember any problematic one - sorry for that; i only remeber that they do exist.. at least using WinXp).
You should use the '-command' switch that as far as i know has no such problems (command line example):

Code: Select all

powershell -command "GWmi CIM_PhysicalMemory | % {($_.devicelocator) + \" = \" + ($_.capacity / 1MB) + \"MB\"}"
powershell -command "GWmi CIM_PhysicalMemory | % {($_.devicelocator) + \" = \" + ($_.capacity / 1GB) + \"GB\"}"

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Ram memory Question?

#7 Post by foxidrive » 24 Sep 2014 04:24

Thanks Compo, that works well.

Thanks penpen too, but I get an error with your code:


At line:1 char:28
+ GWmi CIM_PhysicalMemory | {($_.devicelocator) + " = " + ($_.capacity / 1MB) + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

At line:1 char:28
+ GWmi CIM_PhysicalMemory | {($_.devicelocator) + " = " + ($_.capacity / 1GB) + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Ram memory Question?

#8 Post by Dos_Probie » 24 Sep 2014 04:28

Let me clarify a few things with my code, I am running a Windows RT ARM-based PC (only has a sgl sick of 2GB of ram)
and need to do this from pure batch and not powershell or vbs. I just want to convert over my posted batch to read in GB Much like this below code from here.==> viewtopic.php?f=3&t=5555

Code: Select all

@Echo off
SetLocal
For /f "UseBackQ Tokens=1-2 Delims==" %%A In (`WMIc LogicalDisk Where^
 "DeviceID = '%CD:~,2%'" Get FreeSpace^, Size /Value`) Do If "%%B" NEq "" (
   Call Set _%%A=%%B)
Rem Set/a _FreePercent=%_FreeSpace:~,-3% / %_Size:~,-5%
Set/a _FreePercent=%_FreeSpace:~,-6% / %_Size:~,-8%
If %_FreeSpace:~,-6% lss 214748 (Set/a _FreeSpace=%_FreeSpace:~,-6%*9313/10000
   ) Else (Set/a _FreeSpace=%_FreeSpace:~,-6%/1074*1000)
Echo(HD Drive %CD:~,2% %_FreeSpace:~,-3% GB Of Free Space = %_FreePercent%%%^
 available
Pause>Nul

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Ram memory Question?

#9 Post by penpen » 24 Sep 2014 05:47

@foxidrive
This is strange... , i only would expect such an error (% is within the error message), if running from within a batch file, where you have to use %% (instead of %).
This example is working on WinXP (actual i can't check other OSes).
Which windows do you use?

penpen

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Ram memory Question?

#10 Post by Compo » 24 Sep 2014 06:57

@Dos_Probie, are you sure you need to know the capacity in that single slot?

Would you not be better off determining the amount of physical memory available to the OS:

Code: Select all

@Echo Off & Setlocal
For /F "Tokens=2 Delims==" %%A In ('WMIc OS Get TotalVisibleMemorySize /value'
   ) Do Set/A aRAM=%%A/(1024*1024)
Echo( %aRAM% GB of physical memory is available to the operating system.
Pause>Nul

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Ram memory Question?

#11 Post by foxidrive » 24 Sep 2014 07:31

penpen wrote:@foxidrive
This is strange... , i only would expect such an error (% is within the error message), if running from within a batch file, where you have to use %% (instead of %).


I wasn't wide awake enough to realise it was a command line example.

After doubling the percent sign in the batch file it's fine (Windows 8.1).

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Ram memory Question?

#12 Post by Dos_Probie » 24 Sep 2014 09:04

Thanks Compo for the wmic memory script! but yes instead of showing Available I need it to show the Installed memory (RAM) Which shows that way in system properties. (see updated script below showing single stick)
~DP :D

Code: Select all

@echo off
For /F "Usebackq Delims== Tokens=2" %%x In (`WMIc MemoryChip Get Capacity /Value`
   ) Do Set Inst_Ram=%%x
Set /A KB=%Inst_Ram:~0,-4%
Set /A MB = kb/1024
Set /A GB = mb/1024
Echo(Installed Memory (RAM): %GB%.00 GB
Pause>Nul

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Ram memory Question?

#13 Post by Compo » 24 Sep 2014 13:15

Dos_Probie wrote:(see updated script below showing single stick)
~DP :D

Code: Select all

@echo off
For /F "Usebackq Delims== Tokens=2" %%x In (`WMIc MemoryChip Get Capacity /Value`
   ) Do Set Inst_Ram=%%x
Set /A KB=%Inst_Ram:~0,-4%
Set /A MB = kb/1024
Set /A GB = mb/1024
Echo(Installed Memory (RAM): %GB%.00 GB
Pause>Nul


I don't understand why you would call this solution [SOLVED]!

First as I've already stated it only provides the RAM size for the last stick, anyone with more than one stick of RAM cannot use it. (it may be okay for many ARM machines, but it wouldn't be something to be relied upon)
Additionally, you are using an error/quirk with the output from WMIc in your Math (~0,-4), which makes the arithmetic look wrong.
It would look better like this:

Code: Select all

@Echo Off & SetLocal
For /F "Tokens=2 Delims==" %%A In ('WMIc MemoryChip Get Capacity /Value') Do (
   Call Set iRAM=%%A)
Set/A iRAM=%iRAM:~,-3%/1048576
Echo( Installed Memory (RAM): %iRAM%.00 GB
Pause>Nul

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Ram memory Question? [ SOLVED ]

#14 Post by Squashman » 24 Sep 2014 13:28

Maybe I am not understanding the problem or maybe I do but it seems like using TotalVisibleMemory is the better way to go.

Code: Select all

H:\>WMIc MemoryChip Get Capacity /Value


Capacity=4294967296


Capacity=1073741824




H:\>WMIc OS Get TotalVisibleMemorySize /value


TotalVisibleMemorySize=5193596

My system properties shows 5GB.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Ram memory Question? [ SOLVED ]

#15 Post by Squashman » 24 Sep 2014 13:33

Or this.

Code: Select all

H:\>wmic computersystem get totalphysicalmemory
TotalPhysicalMemory
5318242304

Post Reply