Do Until...LoopAs you know by now, Do...Loop enables the script to continue to perform certain actions until a specific condition occurs. Do While...Loop enables your script to continue to perform these actions as long as the specified condition remains true. Once the specified condition is no longer true, Do While...Loop exits. In contrast, Do Until...Loop has the opposite effectthe script continues to perform the action until a certain condition is met. "So what?" you might ask. In and of itself, Do Until is not all that exciting, but you can use it to perform certain tasks. Here are common uses of Do Until:
Each of these implementations will be used in coming chapters. For now, let's look at a typical use of Do Until, which is illustrated in the ReadTextFile.vbs script: ReadTextFile.vbs Option Explicit 'On Error Resume Next Dim strError Dim objFSO Dim objFile Dim strLine Dim intResult CONST ForReading = 1 strError = "Error" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile("C:\windows\setuplog.txt", ForReading) strLine = objFile.ReadLine Do Until objFile.AtEndofStream strLine = objFile.ReadLine intResult = InStr(strLine, strError) If intResult <> 0 Then WScript.Echo(strLine) End if Loop WScript.Echo("all done") objFile.Close In this script, you begin with the Header information section, which is where you declare your variables and turn on error handling. Here is the Header information section: Option Explicit 'On Error Resume Next Dim strError Dim objFSO Dim objFile Dim strLine Dim intResult As in other scripts, Option Explicit tells VBScript that you're going to tell VBScript about each variable before you use it. If an unnamed item comes up and it's not a command, an error is generated. This helps to save us from misspelled variable names and typos. On Error Resume Next tells VBScript to ignore all the errors it can and to go to the next line. You don't want this turned on when you're writing scripts, because scripts will fail and not let you know what's going on, so it is turned off here. After the two standard lines of the script, it's time to declare some variables. Because you can give variables any name you want (except the names for built-in commands or names already used for constants), it makes sense to use names that are self-explanatory. In addition, as you have already noticed, in VBScript you seem to always be using the same types of connections and commands. For instance, by the end of this book, you will certainly know how to create the file system object, and I tend to use the variable name objFSO for this. The obj part tells me that the item is associated with an object, and the FSO portion is simply shorthand for file system object. This object could just as well be named objFileSystemObject, but I use it a lot and that name requires way too much typing. For some guidance on variable naming conventions, refer to the "Variable Naming Convention" section of Appendix D. Anyway, because this section is not about the file system object but rather about using Do Until, let's plunge ahead. The next part of the script is the Reference information section. It's here that you tell VBScript that you're going to define things to make it easier to work with them. In the following code, you create several reference assignments: CONST ForReading = 1 strError = "error" The constant ForReading is set equal to 1. When you use the openTextFile method, it can open a file in one of three ways: to read, to write, or to append. In this instance, we will open it so we can read from the file. The constant ForReading makes the script easier to read. We will cover this in detail in Chapter 6, "Basic Windows Administration." The strError variable is set equal to the word error. This is what you want to search for in the log file you're going to open. The word assigned to strError can easily be changed to search the log file for other words such as failure, failed, cannot, or even unable to, all of which show up in log files from time to time. By using a variable for the text you are searching for, you are facilitating the ability to change the script to search for other words. Worker and Output InformationYou use two Set commands to talk to the file system and open a text file. We'll be covering these commands in detail in Chapter 6. For now, it's sufficient to note that the text file you're opening to read is C:\windows\setuplog.txt, which is the file that Windows Server 2003 creates during installation. The file is huge and loaded with needed troubleshooting information if setup were ever to fail. But the installation doesn't have to be a complete failure for the file to be useful. For instance, if you're having problems with Windows Product Activation (WPA), just change strError and look for WPA. Error codes found in this section of the setuplog.txt are standard HTTP 1.1 messages (for example, 403 is access denied, 404 is file or directory not found, and 407 is initial proxy authentication required by the Web server). Armed with this information and the script, you can search setuplog.txt, parse the return information, and match it with standard HTTP 1.1 messages. The line strLine = objFile.ReadLine tells VBScript to read one line from the text file referenced by objFile. StrLine holds the line of text that comes out of the file via the ReadLine command. If you printed strLine by using the WScript.Echo command, the line of text would be echoed to the screen. You can also use the strLine variable to hold the line of text so that you can search it for our keyword error. Notice that Do Until is in effect until we are at objFile.AtEndofStream. Think of the ReadLine command as a pumpyou're going to pump text into Do Until...Loop until you reach the end of the text stream. This means that you read lines of text, one line at a time, until you reach the end of the file. You can see this process in the first two lines. Do Until objFile.AtEndofStream strLine = objFile.ReadLine intResult = InStr(strLine, strError) If intResult <>0 Then WScript.Echo(strLine) End if Loop Once the text pump is set up and you have a nice steady stream of letters coming across, you use the next command in the Worker and Output information section of the script. You now use the intResult variable that you declared earlier. You assign intResult to the result of using the InStr command (think of it as "in string"), which looks through a string of text and tries to find a match. The command is put together like this:
In this script, you look through each line of text that comes from the Setuplog.txt file to find the word error, which you assigned to the variable named strError. This part of the script looks like the following: SearchResult = InStr(strLine, strError) Now the situation gets a little complicated, because the InStr command is rather peculiar in the way it hands back information, as detailed in Table 2-1.
In Table 2-1, the only value we're interested in is the one that is not equal to zero. (Although a null value contains no valid data, it is not the same as zero or as the empty string "", often referred to as a null string. You'll learn more about that when we talk about data types in Chapter 8.) To evaluate the results of the InStr function, use If...Then to make sure that what came back from InStr is not equal to zerowhich tells us that InStr is indicating where in the line the word error was found. We really don't care where in the line the word occurs, only that the word is present. You use WScript.Echo to echo out the value of strLine. Note that you print out strLine, which is the variable that contains the line of text that you read from the log file. You don't echo out intResult because it contains only a number, as explained in Table 2-1. After you print out the line containing the error message from the Setuplog.txt file, you end your If statement by using the End If command, and you Loop (which sends us right back to the Do Until command). You continue to Loop Until until you reach the end of the file, at which time you echo out all done and close your file. Echoing all done just lets you know (while you watch stuff scroll on the screen) that you've completed running the script (otherwise, there is no indication that the script completed).
![]() |