Get Free Ram in MB & save it to a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
(_osd_)
Posts: 25
Joined: 01 Mar 2015 07:41

Get Free Ram in MB & save it to a variable

#1 Post by (_osd_) » 09 Aug 2015 08:56

Like the title says, I need a script which saves the free ram aviable IN MB (megabytes) and saves it to a variable.
Thanks alot! :)

npocmaka_
Posts: 516
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Get Free Ram in MB & save it to a variable

#2 Post by npocmaka_ » 09 Aug 2015 09:18

Code: Select all

@echo off
for  /f "usebackq" %%a in (`mshta ^"javascript^:close^(new ActiveXObject^(^'Scripting.FileSystemObject^'^).GetStandardStream^(1^).Write^(GetObject^(^'winmgmts:^'^).ExecQuery^(^'Select * from Win32_PerfFormattedData_PerfOS_Memory^'^).ItemIndex^(0^).AvailableMBytes ^)^);^"`) do set free_mem=%%a
echo %free_mem%

Meerkat
Posts: 89
Joined: 19 Jul 2015 02:27
Location: Philippines

Re: Get Free Ram in MB & save it to a variable

#3 Post by Meerkat » 09 Aug 2015 21:58

I am sorry at first. I thought this is somewhat like a spam! :mrgreen:

BTW, this is the same thing, but without that "little HTA flash" thing...

Code: Select all

@echo off
for  /f "usebackq" %%a in (`mshta ^"javascript^:code^(close^(new ActiveXObject^(^'Scripting.FileSystemObject^'^).GetStandardStream^(1^).Write^(GetObject^(^'winmgmts:^'^).ExecQuery^(^'Select * from Win32_PerfFormattedData_PerfOS_Memory^'^).ItemIndex^(0^).AvailableMBytes^)^)^); ^"`) do set free_mem=%%a
echo %free_mem%
pause


Meerkat

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: Get Free Ram in MB & save it to a variable

#4 Post by taripo » 10 Aug 2015 06:57

well, tasklist will list

Code: Select all

chrome.exe                    6604 Console                    1    341,108 K
chrome.exe                    4220 Console                    1     74,548 K
chrome.exe                    7256 Console                    1     52,876 K
chrome.exe                    8084 Console                    1     35,156 K
vncviewer.exe                 8056 Console                    1     23,904 K


It's possible an administrative cmd prompt lists more.. one would have to check..

I am not sure whether those numbers are so-called binary KB i.e. 1024 bytes. Or decimal ones i.e. 1000 bytes. I guess 1024.

I wrote a messy program once that adds them up..

It requires gnuwin32 sed, coreutils(cut,tail) adding gnuwin32\bin to your path

It gives a result like

Code: Select all

C:\>ramall
>=1GB
3GB
3,693,816
3.6GB (rounded down)
3693816    KB

C:\>



So it looks at all the RAM used, it tells you it's in the range >=1GB so you have some idea of magnitude. It used to be just for chrome, though for all ram it's not that necessary to say >=1GB.

Then it gives the number in KB..

tells you what it is rounded down in GB (that's probably an error because I took the KB and divided it by 1000. I probably shouldve divided it by 1024 but anyway).

And it shows you the total KB

Just the sum of all the numbers given by tasklist

You can also do a test by doing tasklist >a.a and getting those numbers in excel, though the comma(in tasklist's results) may get in the way, you can remove that.

In fact you can probably do the whole thing in a batch without any tools like sed.


Look at the tasklist results

extract the numbers into a file so you get a file that has

3848
6720
72252
3680
5736
7560
3552
5728
5868

Then you do something like this

set total=0
for /f %%f in (%file%) do @(set /a total+=%%f)>nul

And then total will have the total of all those numbers in that file. Each number is in KB so the total will be in KB.

Not difficult, and my script did that though it was a bit of a nutty professor program

And it has to be the gnuwin32 tail not the windows tail

Code: Select all

@echo off
setlocal enableextensions enabledelayedexpansion

:: look for gnuwin32, with cut(coreutils) , sed, and tail. and not windows's tail.

:: I put the 2>1 because in a test, tail.bat had the letters df in there which is a command in linux, in gnuwin32!

set error=false
:: @tail --version 2>1 | find /i "GNU" >nul
:: if %errorlevel%==1 set error=true

@cut --help >nul 2>nul
if NOT %errorlevel%==0 set error=true

@sed --help >nul 2>nul
if NOT %errorlevel%==0 set error=true

if %error%==true (
   echo one of these isn't there.  gnu tail,sed,cut. check that gnuwin32
   echo is installed and coreutils package, and sed package, so you have
   echo those three^(gnu tail,cut,sed^), and that they're in the path
   set error=
   exit /b
)
set error=


:: set pathofgnuwin32="c:\program files (x86)\gnuwin32\bin"



set tempfile1=%temp%\asdf1.a
set tempfile2=%temp%\asdf2.a
set tempfile3=%temp%\asdf3.a
set tempfile4=%temp%\asdf4.a

tasklist >%tempfile1%
type %tempfile1% | tail +4 > %tempfile2%

cut -b 68- %tempfile2% > %tempfile3%
sed -r "s/\d32|K|,//g" %tempfile3% >%tempfile4%


set total=0
for /f %%f in (%tempfile4%) do @(set /a total+=%%f)>nul




:: ------------
:: --- added this
set a=%total%


:: ----------------------------------------------------------------
if "%a:~-9,-6%"=="" echo ^< 1GB
if NOT "%a:~-13,-6%"=="" echo ^>=1GB

set yotta=%a:~-24,-21%
set zetta=%a:~-21,-18%
set exa=%a:~-18,-15%
set peta=%a:~-15,-12%
set tera=%a:~-12,-9%
set giga=%a:~-9,-6%
set mega=%a:~-6,-3%
set kilo=%a:~-3%

set prefix=

if NOT "!yotta!"=="" (
   set yotta=!yotta!,
    if "!prefix!"=="" (
       set prefix=YB
       echo !a:~-24,-21!!prefix!
    )
)

if NOT "!zetta!"=="" (
   set zetta=!zetta!,
   if "!prefix!"=="" (
      set prefix=ZB
      echo !a:~-21,-18!!prefix!
    )
)

if NOT "!exa!"=="" (
   set exa=!exa!,
   if "!prefix!"=="" (
      set prefix=EB
      echo !a:~-18,-15!!prefix!
    )
)

if NOT "!peta!"=="" (
   set peta=!peta!,
   if "!prefix!"=="" (
      set prefix=PB
      echo !a:~-15,-12!!prefix!
    )
)

if NOT "!tera!"=="" (
    set tera=!tera!,
    if "!prefix!"=="" (
      set prefix=TB
      echo !a:~-12,-9!!prefix!
    )
)

if NOT "!giga!"==""  (
    set giga=!giga!,
    if "!prefix!"=="" (
      set prefix=GB
      echo !a:~-9,-6!!prefix!
    )
)

if NOT "!mega!"==""  (
    set mega=!mega!,
    if "!prefix!"=="" (
      set prefix=MB
      echo !a:~-6,-3!!prefix!
    )
)

if "!mega!"=="" (
  if "!prefix!"=="" (
   set prefix=KB
   echo !kilo!
  )
)


echo !yotta!!zetta!!exa!!peta!!tera!!giga!!mega!!kilo!

if "%prefix%"=="YB" echo !a:~-24,-21!.!a:~-21!YB (rounded down)
if "%prefix%"=="TB" echo !a:~-12,-9!.!a:~-9,1!TB  (rounded down)
if "%prefix%"=="GB" echo !a:~-9,-6!.!a:~-6,1!GB (rounded down)
if "%prefix%"=="MB" echo !a:~-6,-3!.!a:~-3,1!MB (rounded down)



 set a=
 
:: echo %a:~-13,-6%,%a:~-6,-3%,%a:~-3%
:: -----------------------------------------------------


echo %total%    KB


set tempfile1=
set tempfile2=
set tempfile3=
set tempfile4=

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: Get Free Ram in MB & save it to a variable

#5 Post by taripo » 10 Aug 2015 07:19

further to my last post, here's how you do it in a nutshell

using 3rd party tools of sed and grep

Code: Select all

C:\blah>tasklist>a.a

C:\blah>del b.b

C:\blah>for /f "tokens=5 delims= " %f in (a.a) do @echo %f >>b.b

C:\blah>sed "s/,//" b.b>c.c

C:\blah>grep -P "^\d+" c.c>d.d

C:\blah>set total=0

C:\blah>for /f %f in (d.d) do @(set /a total+=%f)>nul

C:\blah>echo %total%
3699432

C:\blah>



3699432KB

which is 3.6GB

If you wanted it in MB

Code: Select all

C:\blah>echo %total%
3699432

C:\blah>set /a totalmb=%total%/1024
3612
C:\blah>




so now the total in MB is in the totalmb variable.

(3000MB is approx 3GB which is correct for the usage on the machine i'm on)

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

Re: Get Free Ram in MB & save it to a variable

#6 Post by Compo » 10 Aug 2015 13:07

Just a note to remind you that once you've captured the variable data, it will have already changed, so it will only be pertinent to that exact capture time and will of course have been affected by the process which was used to capture it.

Here's the WMI Command Line version:

Code: Select all

@Echo Off
SetLocal
For /F "Tokens=2 Delims==" %%A In (
   'WMIc Path Win32_PerfFormattedData_PerfOS_Memory Get AvailableMBytes /Value'
) Do For %%B In (%%A) Do Set MBMFree=%%B
Echo=[%%MBMFree%%=%MBMFree%]
Pause>Nul

My slightly altered HTA version:

Code: Select all

@Echo Off
SetLocal
For /F "UseBackQ" %%A In (
   `MsHTA ^"JavaScript:Code(close(new ActiveXObject('Scripting.FileSystemObject'^).GetStandardStream(1^).Write(GetObject('WinMgmts:'^).ExecQuery('Select AvailableMBytes From Win32_PerfFormattedData_PerfOS_Memory'^).ItemIndex(0^).AvailableMBytes^)^)^);^"`
) Do Set MBMFree=%%A
Echo=[%%MBMFree%%=%MBMFree%]
Pause>Nul

And for no real reason other than 'it's likely to irritate some', a couple of Powershell ones:

Code: Select all

@Echo Off
SetLocal
For /F %%A In (
   'PowerShell -C (GWMI -Class Win32_PerfFormattedData_PerfOS_Memory^).AvailableMBytes'
) Do Set MBMFree=%%A
Echo=[%%MBMFree%%=%MBMFree%]
Pause>Nul

Code: Select all

@Echo Off
SetLocal
For /F "UseBackQ" %%A In (
   `PowerShell -C "Get-Counter -Counter '\Memory\Available MBytes'|%%{$_.CounterSamples[0].CookedValue}"`
) Do Set MBMFree=%%A
Echo=[%%MBMFree%%=%MBMFree%]
Pause>Nul

Post Reply