Microsoft® Windows® 2000 Scripting Guide
The Do loop allows you to repeatedly run a set of statements until a specified condition has been met. For example, you might want to periodically monitor free disk space until the amount of free disk space falls below a specified value. Likewise, you might want to run some sort of disk cleanup process as long as available memory remains above a certain level; if memory drops below that level, you might end the cleanup process as a way to regain memory. The Do loop allows you to run loops for an indefinite number of times. (The For Next loop, by contrast, requires that the loop be run a specific number of times.)
VBScript supports two types of Do loops: Do Until and Do While. The Do Until loop continues running a loop until a specific condition is met. This generally means that the loop runs until the loop condition becomes True. For example, the following script uses the FileSystemObject to open a text file and then read in the text file line by line. To do this, the code for reading the text file is placed in a loop that will continue to run until the end of the file (the text stream) has been reached. In the following script, the loop condition has the following syntax:
Do Until objTextFile.AtEndOfStream
This is read as, "Continue looping until the end of the file is reached."
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:\test.txt", ForReading) Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline Wscript.Echo strNextLine Loop
When the preceding script runs, the loop condition is checked each time to see whether the end of the file has been reached. If it has, the loop will not run. If it has not, the loop will run, and the next line of the file will be read.
By contrast, the Do While loop runs as long as the condition has not been met (that is, as long as it is False). The following script uses the syntax:
Do While Not objTextFile.AtEndOfStream
This is read as, "Continue looping as long as the end of the text file has not been reached." If the text file still has unread lines, this loop will continue to run.
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("c:\test.txt", ForReading) Do While Not objTextFile.AtEndOfStream strNextLine = objTextFile.Readline Wscript.Echo strNextLine Loop
Creating Endless Loops
As you might expect, it is possible to create an endless loop, one that never stops running. (If the condition is never met, the loop will continue to run forever.) This is often a mistake on the part of the script writer. However, suppose you have a script that alerts you each time an error event is recorded in the event log. You do not want that loop to stop after the first alert has been sent; instead, you would want the loop to continue and allow you to continue to receive these notifications.
To create a loop that runs forever, you simply specify a loop condition that will never be met. For example, in the following script fragment, the loop is designed to run until LoopVariable is equal to 0. Because the script does nothing to change the value of LoopVariable, this condition will never be met, and the loop will continue to run indefinitely.
LoopVariable = 1 Do Until LoopVariable = 0 FreeMegaBytes = objLogicalDisk.FreeSpace / CONVERSION_FACTOR If FreeMegaBytes < WARNING_THRESHOLD Then Wscript.Echo Computer & " " & objLogicalDisk.DeviceID & _ " is low on disk space." End If Loop
The only way stop this loop is to stop the script.