For...Next

上一篇 / 下一篇  2010-03-23 11:06:32 / 个人分类:VBScript

For...Next VS For Each...Next

An important difference between For Each...Next and For...Next is that with For Each...Next, you don't have to know how many times you want to do something. With the For...Next construct, you must know exactly how many times you want to do something.

 

例子DisplayProcessInformation.vbs

'Header information section,define the variabl and on error resume Next
Option Explicit
On Error Resume Next
Dim objWMIService
Dim objItem
Dim i

'reference information sectuin,define two constants
Const MAX_LOOPS = 8, ONE_HOUR = 3600000

'Worker and Output Information, connects to WMI and then executes a query
'that lists all Win32 processes running on the machine
'query chooses everything from the Win32 process, but only if the process ID is not equal to 0
'(the system idle process)

For i = 1 To MAX_LOOPS
Set bjWMIService = GetObject("winmgmts:").ExecQuery _
  ("SELECT * FROM Win32_Process where processID <> 0")
WScript.Echo "There are " & objWMIService.count &_
 " processes running " & Now
 For Each objItem In objWMIService
    WScript.Echo "Process: " & objItem.Name
    WScript.Echo Space(9) & objItem.commandline
    WScript.Echo "Process ID: " & objItem.ProcessID
    WScript.Echo "Thread Count: " & objItem.ThreadCount
    WScript.Echo "Page File Size: " & objItem.PageFileUsage
    WScript.Echo "Page Faults: " & objItem.PageFaults
    WScript.Echo "Working Set Size: " & objItem.WorkingSetSize
    WScript.Echo vbNewLine  ' add a blank line
  Next
  WScript.Echo "******PASS COMPLETE**********"
  WScript.Sleep ONE_HOUR    'wscript. sleep method, the time expression with milliseconds
Next

 

 

From
Microsoft® VBScript. Step by Step
By Ed Wilson


TAG:

 

评分:0

我来说两句

Open Toolbar