Livecode and disk usage.

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Livecode and disk usage.

Post by zaxos » Tue Jul 22, 2014 5:24 pm

Hello, is there a way to get the disk usage and maybe cpu usage with livecode?
Thank you for your time.
Knowledge is meant to be shared.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Livecode and disk usage.

Post by FourthWorld » Tue Jul 22, 2014 7:56 pm

IIRC the freesize function returns available disk space on the volume in which the directory is currently set.

For CPU or RAM statistics you'll need to call out to the OS via shell.

On Linux this stuff is easy: for processes use "top" with a one-iteration argument and parse the results, and for RAM use "free".

Top will also work on Mac, but not free; you'll have to dig up the Mac equivalent for that. On Windows the command line is so FUBAR that you'll need to use other calls, and I don't recall what they are only that it was much more onerous (as is nearly everything in an OS that doesn't ship with bash).
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm

Re: Livecode and disk usage.

Post by paul_gr » Tue Jul 22, 2014 11:47 pm

Since Windows XP Pro you can use WMIC to get a LOT of details about Windows and the computer it is running on.

example -- Windows Only...
drop one field on the card and name it "fldOut"
put the script below on a button.
-------------------------------------------------------------------------
on mouseUp
set the hideConsoleWindows to true
put empty into fld "fldOut"

put "wmic cpu get loadpercentage" into tCom1
put "CPUload percent is " & line 3 of shell(tCom1) into line 1 of fld "fldout"

put "wmic os get freephysicalmemory" into tCom2
put "freephysicalmemory is " & line 3 of shell(tCom2) into line 2 of fld "fldout"

end mouseUp
-------------------------------------------------------------------------

this will get the CPU load and the free memory details.

Paul

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Livecode and disk usage.

Post by FourthWorld » Wed Jul 23, 2014 12:12 am

Is WMIC available in the "Home" (non-"Pro") versions of Windows?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm

Re: Livecode and disk usage.

Post by paul_gr » Wed Jul 23, 2014 12:17 am

Hi Richard,
FourthWorld wrote:Is WMIC available in the "Home" (non-"Pro") versions of Windows?
In Windows 7 and 8 -- yes.
In XP, the pro version is needed.

Paul
Last edited by paul_gr on Wed Jul 23, 2014 12:24 am, edited 2 times in total.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10058
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Livecode and disk usage.

Post by FourthWorld » Wed Jul 23, 2014 12:22 am

paul_gr wrote:Hi Richard,
FourthWorld wrote:Is WMIC available in the "Home" (non-"Pro") versions of Windows?
In Windows 7 and 8 -- yes.
In XP, the pro version is needed.
Nice. Not as nice as Cygwin, but if it doesn't need to be installed it's super-useful. Thanks.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Livecode and disk usage.

Post by zaxos » Wed Jul 23, 2014 12:47 am

Super ! this worked like a charm, never thought of shell since its blocking my whole stack, is there a way to overcome this?
Thank you again for your time !
Knowledge is meant to be shared.

paul_gr
Posts: 319
Joined: Fri Dec 08, 2006 7:38 pm

Re: Livecode and disk usage.

Post by paul_gr » Wed Jul 23, 2014 2:17 am

Hi Zaxos,
shell() is a blocking command, unfortunately.
One possible thing to watch for when using shell() in a corporate setting; some sysadmins block use of shell(), by disabling cmd.exe...

Paul

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Livecode and disk usage.

Post by zaxos » Wed Jul 23, 2014 6:02 pm

Code: Select all

put "wmic cpu get loadpercentage > c:\log.log" into cpuLog
launch cpuLog
is there a reason this dosent work??

Code: Select all

put "cmd wmic cpu get loadpercentage > c:\log.log" into cpuLog
launch cpuLog
or this

Code: Select all

put "wmic cpu get loadpercentage > c:\log.log" into cpuLog
launch cpuLog with "cmd"
or even this
Knowledge is meant to be shared.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Livecode and disk usage.

Post by Klaus » Wed Jul 23, 2014 6:36 pm

Hi zaxos,

because LAUNCH is not the correct syntax, as you might have guessed after reading the other postings :D

Do this:
...
put "cmd wmic cpu get loadpercentage > c:\log.log" into cpuLog
put SHELL(cpuLog) into tTemp
...


Best

Klaus

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Livecode and disk usage.

Post by zaxos » Wed Jul 23, 2014 6:44 pm

how bout this?

Code: Select all

 // wmic cpu get loadpercentage > c:\log.log in script.bat
launch "C:\script.bat"
whys that not working too ? :/
Knowledge is meant to be shared.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Livecode and disk usage.

Post by Klaus » Wed Jul 23, 2014 6:50 pm

Hi zaxos,

please check the dictionary, if yomething does not works as you exspect! 8)

Code: Select all

launch "C:\an_app.EXE"
But you want:

Code: Select all

launch DOCUMENT "C:\script.bat"

Best

Klaus

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Livecode and disk usage.

Post by zaxos » Wed Jul 23, 2014 6:55 pm

nice ! well that solved it :D, no more blocking and with a read from file i get an endless reading :), thank you man, and btw the dictinary usualy cofuzed me :/
Knowledge is meant to be shared.

Klaus
Posts: 14213
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Livecode and disk usage.

Post by Klaus » Wed Jul 23, 2014 7:10 pm

zaxos wrote:... and btw the dictinary usualy cofuzed me :/
Try to make the dictionary your friend, it may be your only companion in dark, cold winter nights!
:D :D :D

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Livecode and disk usage.

Post by zaxos » Wed Jul 23, 2014 7:55 pm

for some reason i couldent hide the cmd this way :(, anyway i found a workaround with vbs script , here it is if anyone need it
Livecode:

Code: Select all

local vCancel
on mouseUp
   if vCancel is true then
      put false into vCancel
      cpuUsage
      set the label of btn "cpuStatus" to "Stop"
   else
      put true into vCancel
      set the label of btn "cpuStatus" to "Start"
   end if
   
end mouseUp

on cpuUsage
   put "C:\cpuUsage.vbs" into theCMD
   launch document theCMD
   wait 100 millisec
   put URL "file:C:\cpuUsage.txt" into log
   put log into field 1
   set itemDel to cr
   set the thumbposition of scrollbar 1 to item 2 of log
   put empty into URL "file:C:\cpuUsage.txt"
   if vCancel is true then
      -- nothing
   else
      send cpuUsage to me in 1 sec
   end if
end cpuUsage
cpuUsage.vbs

Code: Select all

Option Explicit 
  
Dim oWsh, oWshSysEnv, objFSO, objWMIService 
Dim oDrives, oDrive, objOutFile, colItems, objItem 
Dim strLineTime, strLineProcessorTime, strLineDriveSpace, strLinePercentCommittedBytesInUse 
  
Set oWsh = WScript.CreateObject("WScript.Shell") 
Set oWshSysEnv = oWsh.Environment("PROCESS") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
  
strLineTime = Date() & Space(2) & Time() 
  
'Gets PROCESSOR Usage 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'") 
For Each objItem In colItems    
 strLineProcessorTime = strLineProcessorTime & " " & objItem.PercentProcessorTime 
Next 

'Output to text 
Set objOutFile = objFSO.OpenTextFile("C:\cpuUsage.txt", 8, True) 
objOutFile.WriteLine "Processor Usage (%)" 
objOutFile.WriteLine strLineProcessorTime
  
WScript.Quit 
all you have to do is make a stack with a button named cpuUsage, a scrollbar and a field, this code updates both the field and the scrollbar and it only cancels if u press the button again :)
Knowledge is meant to be shared.

Post Reply