Microsoft® Windows® 2000 Scripting Guide
One way to get a single script to run the same set of commands over and over is to enclose those commands within a For Next loop, which allows you to run lines of code a specified number of times.
For example, the script shown in Listing 2.9 checks free disk space on a computer every hour for 12 hours. To do this, a For statement is used on line 5 to indicate that the enclosed code block should be run 12 times. Lines 6-10 include the code required to determine the amount of free space for each disk drive on the computer, and line 11 pauses the script for one hour (using a constant that pauses the script for 3,600,000 milliseconds). Line 12 is simply the Next statement, which marks the end of the loop.
When the script runs, a connection is made to the remote computer atl-dc-01. The script retrieves the free disk space information and then pauses for one hour. After that hour, the script returns to the first statement of the For Next loop and retrieves free disk space information for a second time. This continues until the disk space information has been retrieved 12 times. After that, the script runs the line of code following the Next statement. Because no lines of code follow that statement, the script completes.
Listing 2.9 Running Commands Multiple Times
1 2 3 4 5 6 7 8 9 10 11 12
Const CONVERSION_FACTOR = 1048576 Const ONE_HOUR = 3600000 Computer = "atl-dc-01" Set objWMIService = GetObject("winmgmts://" & Computer) For i = 1 to 12 Set colLogicalDisk = objWMIService.InstancesOf("Win32_LogicalDisk") For Each objLogicalDisk In colLogicalDisk FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR Wscript.Echo objLogicalDisk.DeviceID & " " & Int(FreeMegaBytes) Next Wscript.Sleep ONE_HOUR Next
The For Next statement allows you to run a block of code a specific number of times. This should not be confused with a For Each statement. For Each is used to iterate through the individual items within a collection. For Next is used to run a particular set of statements a specified number of times.
To use a For Next statement, you must determine both a starting point and an ending point. Because For Next statements are typically designed to run a set of statements X number of times, you will generally start with 1 and end with X. Therefore, to do the same thing 10 times, you start with 1 and end with 10.
You can pick an arbitrary starting point (for example, 314 or 6,912) and then conclude with the appropriate end point (324 or 6,922). However, your code will be easier to read and maintain if you start with 1 and end with 10.
The For Next statement requires you to use a loop variable (also known as a counter) that keeps a running tally of how many times the code has run. For example, the variable i is used as the counter in the following code sample. The counter starts at 1 and runs the lines of code contained within the For Next statement block. After all the statements have run, the counter is automatically incremented by 1, meaning i is now equal to 2. The script loops back to the beginning of the For Next statement and checks to see whether the value 2 is still within the valid execution range. Because it is, the code within the For Next statement block runs a second time.
For i = 1 to 5 Wscript.Echo i Next Wscript.Echo "For Next loop complete."
What happens when i is equal to 6? The script will loop back to the beginning of the For Next statement, and check to see if 6 is part of the valid execution range. Because it is not, the For Next statement will immediately stop, and running of the script will continue with the first line following the Next statement. In this case, that is the line that echoes the message "For Next loop complete."
The script output looks like this:
1 2 3 4 5 For Next loop complete.
There are times when you want to run the same set of statements over and over; however, you might have no way of determining in advance how many times you need to run that code. For example, suppose you want to check free disk space once every hour and continue to run this check until disk space drops below a specified amount. In a case such as this, you want the script to start and continue to run until disk space has dropped below the threshold, regardless of how many iterations that requires. In these situations, you should use a Do Loop, discussed later in this chapter.