cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted

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
Highlighted
Intrepid explorer

Re: Parsing string

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
Highlighted

Re: Parsing string

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
Highlighted
Intrepid explorer

Re: Parsing string

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
Highlighted

Re: Parsing string

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
Highlighted

Re: Parsing string

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
Highlighted
Intrepid explorer

Re: Parsing string

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
Highlighted
Intrepid explorer

Re: Parsing string

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
Highlighted
Intrepid explorer

Re: Parsing string

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
Highlighted

Re: Parsing string

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