Flash Tip: October 4, 2006
Flash Tip: Statement Breaks, For Next Statements, and Using Do Loops in VBScript Code
By Microsoft Corporation
Q: What are the situations when I should use statement breaks, For Next statements, and Do loops in VBScript code?
A: In Microsoft Visual Basic Scripting Edition (VBScript), the underscore (_) at the end of a line of code is known as the line continuation character and is used to indicate a statement break. This means that the line on which the underscore appears and the following line should be treated as one line. The line was simply too long to fit in the allotted space.
To use a For Next statement, you must determine both a starting point and an ending point. Because For Next statements typically 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 ending point (324 or 6,922). However, your code will be easier to read and maintain if you start with 1 and end with 10.
There are times when you want to run the same set of statements over and over, but you might not have a way to determine 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 cases such as this one, 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.
Based on “VBScript Primer" from the book Windows 2000 Scripting Guide by Microsoft Corporation. Reprinted by permission of Microsoft Press. All rights reserved. For more information, go to http://www.microsoft.com/mspress/books/6417.asp.