Page 1 of 2

Ram memory Question? [ SOLVED ]

Posted: 23 Sep 2014 19:08
by Dos_Probie
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

Re: Ram memory Question?

Posted: 23 Sep 2014 19:57
by ShadowThief
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).

Re: Ram memory Question?

Posted: 23 Sep 2014 19:59
by Compo
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"}

Re: Ram memory Question?

Posted: 23 Sep 2014 21:10
by foxidrive
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

Re: Ram memory Question?

Posted: 24 Sep 2014 03:09
by Compo
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'}"

Re: Ram memory Question?

Posted: 24 Sep 2014 04:04
by penpen
@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

Re: Ram memory Question?

Posted: 24 Sep 2014 04:24
by foxidrive
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

Re: Ram memory Question?

Posted: 24 Sep 2014 04:28
by Dos_Probie
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

Re: Ram memory Question?

Posted: 24 Sep 2014 05:47
by penpen
@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

Re: Ram memory Question?

Posted: 24 Sep 2014 06:57
by Compo
@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

Re: Ram memory Question?

Posted: 24 Sep 2014 07:31
by foxidrive
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).

Re: Ram memory Question?

Posted: 24 Sep 2014 09:04
by Dos_Probie
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

Re: Ram memory Question?

Posted: 24 Sep 2014 13:15
by Compo
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

Re: Ram memory Question? [ SOLVED ]

Posted: 24 Sep 2014 13:28
by Squashman
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.

Re: Ram memory Question? [ SOLVED ]

Posted: 24 Sep 2014 13:33
by Squashman
Or this.

Code: Select all

H:\>wmic computersystem get totalphysicalmemory
TotalPhysicalMemory
5318242304