Hi,
I just recently ran into situation I needed to retrieve list of processes running in memory + their CPU usage and save it to file. The following script did the job:
Uses "WMI", "OS", "Console", "File"
PrintL "Retrieving information, please wait..."
' -- The following code returns query to string buffer
' -- We request only Name and PercentProcessorTime fields, but more is possible, see:
' -- http://msdn.microsoft.com/en-us/library/aa394277%28v=vs.85%29.aspx
String sBuffer = WMI_GetData(OS_GetComputerName, "", "", "", "Win32_PerfFormattedData_PerfProc_Process", "", "Name,PercentProcessorTime")
String sBufferOut
' -- Number of processes found equals to number of Element "tags"
Long nProcesses = Tally(sBuffer, "{Element")
Long i
String sName, sUsage, sLine
' -- File to which we retrieve info
String sFile = APP_SourcePath+"ProcessList.txt"
For i = 1 To nProcesses
' -- Grab$ is great for retrieving fields determined by different "tags"
sName = Grab$(sBuffer, "Name=", $CRLF, i)
sUsage = Grab$(sBuffer, "PercentProcessorTime=", $CRLF, i)
' -- We leave out items Idle and _Total, but you can change this to your needs
If sName = "Idle" Or sName = "_Total" Then Iterate For
sLine = LSet$(sName, 30 Using " ") + sUsage+"%"
sBufferOut += sLine + $CRLF
PrintL sLine
Next
FILE_Save(sFile, sBufferOut)
PrintL
PrintL "File saved as " + sFile
PrintL "Press any key to quit..."
WaitKey
It can be done via Win32 as well, but the code gets quite long, while using WMI it was just few lines of code.
The version I used was 3 lines long, but the version above is more readable
Original code was:
Uses "WMI", "OS", "File"
FILE_Save(APP_SourcePath+"ProcessDump.txt", WMI_GetData(OS_GetComputerName, "", "", "", "Win32_PerfFormattedData_PerfProc_Process", "", "Name,PercentProcessorTime"))
and resulted in such a log:
Computer:{PETR-TOSH}@Action:{Win32_PerfFormattedData_PerfProc_Process}
{Element:1|82}
Name=Idle
PercentProcessorTime=100
{Element:2|82}
Name=System
PercentProcessorTime=0
{Element:3|82}
Name=smss
PercentProcessorTime=0
{Element:4|82}
Name=csrss
PercentProcessorTime=0
...
So you can see why I picked the GRAB$ in tuned version to make it more readable.
Petr
Bookmarks