cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dinesh_redhawk
Level 6

Parsing string

While executing the below code in cmd prompt :
systeminfo | findstr /C:”Total Physical Memory”

it returns the string
“Total Physical Memory:     8,072 MB”


How can i get JUST 8072 ?

parsing Total Physical Memory: , the comma, spaces and MB?

Please help.
I am getting confused and failed multiple times by using strtrim, lists, strgettoken etc...
Labels (1)
0 Kudos
(15) Replies
rguggisberg
Level 13

One way to do it is to replace

systeminfo | findstr /C:”Total Physical Memory”

With this

for /f "usebackq tokens=4-5 delims=:, " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do echo(%%A%%B


Note that this is a simple method that works if there is only 1 comma. If there is the possibility of multiple commas I would do it differently.
0 Kudos
dinesh_redhawk
Level 6

rguggisberg wrote:
One way to do it is to replace

systeminfo | findstr /C:”Total Physical Memory”

With this

for /f "usebackq tokens=4-5 delims=:, " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do echo(%%A%%B


Note that this is a simple method that works if there is only 1 comma. If there is the possibility of multiple commas I would do it differently.


Thanks, but in my case we need to handle multiple comma scenario. 😞
0 Kudos
rguggisberg
Level 13

dinesh_redhawk wrote:
Thanks, but in my case we need to handle multiple comma scenario. 😞


OK... just gets a bit more complicated 🙂


setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal
0 Kudos
dinesh_redhawk
Level 6

rguggisberg wrote:
OK... just gets a bit more complicated 🙂


setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal


How to run this code? if i run it through CMD, its returning the following error:

%%A was unexpected at this time.
0 Kudos
dinesh_redhawk
Level 6

dinesh_redhawk wrote:
How to run this code? if i run it through CMD, its returning the following error:

%%A was unexpected at this time.


Ok, i tried running the command in Bat file and its giving the following output:

set "Memory=8,072" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal
8072

I need only 8072 to be written in a temp txt file, so that i can read it.
Appreciate your prompt help.

Thanks a lot.
0 Kudos
rguggisberg
Level 13

dinesh_redhawk wrote:
How to run this code? if i run it through CMD, its returning the following error:

%%A was unexpected at this time.


If running via CMD you need to change BOTH %%A to %A
0 Kudos
rguggisberg
Level 13

dinesh_redhawk wrote:
Ok, i tried running the command in Bat file and its giving the following output:

set "Memory=8,072" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal
8072

I need only 8072 to be written in a temp txt file, so that i can read it.
Appreciate your prompt help.

Thanks a lot.


Works in my bat file. Can you post yours? Sometimes that violates the security policy of this site 😞
We can write it to a file.but need to fix current problem first.
0 Kudos
rguggisberg
Level 13

Change code to this

setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal


I will post code to write to file in a bit.
0 Kudos
dinesh_redhawk
Level 6

rguggisberg wrote:
Works in my bat file. Can you post yours? Sometimes that violates the security policy of this site 😞
We can write it to a file.but need to fix current problem first.


Please find the attached bat file.

Please rename the extension from txt to bat file.

Thanks
0 Kudos
dinesh_redhawk
Level 6

rguggisberg wrote:
Change code to this

setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal


I will post code to write to file in a bit.


This is also returning the following output 😞

set "Memory=8,072" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal
8072
0 Kudos
rguggisberg
Level 13

dinesh_redhawk wrote:
This is also returning the following output 😞

set "Memory=8,072" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal
8072


Sorry 😞
Last code should have been

@echo off & setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal


Code to write to file is

@echo off & setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! >"%Tempfile%" & endlocal


You need to replace %Tempfile% above with the path\filename of your desired file
0 Kudos
dinesh_redhawk
Level 6

rguggisberg wrote:
Sorry 😞
Last code should have been

@echo off & setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! & endlocal


Code to write to file is

@echo off & setlocal enabledelayedexpansion & for /f "usebackq tokens=4-5 delims=: " %%A in (`systeminfo ^| findstr /C:"Total Physical Memory"`) do set "Memory=%%A" & set "Memory=!Memory:,=!" & ECHO(!Memory! >"%Tempfile%" & endlocal


You need to replace %Tempfile% above with the path\filename of your desired file


This worked... U are Amazing sir. I am very thankful to you for helping me so promptly.

One final question: Can the same be done for this command? wmic computersystem get TotalPhysicalMemory
Because the one we are using currently is taking some few seconds to return the result, while this command returns the result immediately. I know iam stretching it too much, but just wanted to know.
0 Kudos
rguggisberg
Level 13

dinesh_redhawk wrote:
This worked... U are Amazing sir. I am very thankful to you for helping me so promptly.

One final question: Can the same be done for this command? wmic computersystem get TotalPhysicalMemory
Because the one we are using currently is taking some few seconds to return the result, while this command returns the result immediately. I know iam stretching it too much, but just wanted to know.


Depends. If you can live with the long number, yes.

@ECHO OFF & for /f %%A in ('wmic computersystem get TotalPhysicalMemory') do if not "%%A"=="TotalPhysicalMemory" echo(%%A >>"%Tempfile%"


Of course you need to replace %TempFile% above as needed.

Note that we have to use >> to append to the file. This is because wmic ouputs a LF or something at the end that the FOR loop does not interpret as an End Of Line... so we can't just re-write the file for each iteration of the loop. Because of this you will have to delete the temp file if it is present before you run this (good practice anyway).
0 Kudos
dinesh_redhawk
Level 6

So finally thanks to rguggisberg and my research.

found 2 possible solutions to this issue:
Please find the attached bat files. Rename the txt extension to bat extn.

RAM1 solution will work for all varieties of computer with multiple Dimm slots. CON: it takes few seconds to produce the output which is okay.

RAM2 solution (wmic memorychip get capacity) gives you the capacity of each Dimm (which will work fine, if you have only one Dimm installed but gives you one value per Dimm if there is installed more than one). It instantly gives the result.

To get the size of total memory use RAM1(wmic computersystem get TotalPhysicalMemory).
CON: it takes few seconds to produce the output which is okay.

0 Kudos
dinesh_redhawk
Level 6

rguggisberg wrote:
Depends. If you can live with the long number, yes.

@ECHO OFF & for /f %%A in ('wmic computersystem get TotalPhysicalMemory') do if not "%%A"=="TotalPhysicalMemory" echo(%%A >>"%Tempfile%"


Of course you need to replace %TempFile% above as needed.

Note that we have to use >> to append to the file. This is because wmic ouputs a LF or something at the end that the FOR loop does not interpret as an End Of Line... so we can't just re-write the file for each iteration of the loop. Because of this you will have to delete the temp file if it is present before you run this (good practice anyway).


You are great sir 🙂 Appreciate your help. Saved my day
0 Kudos