Ask:
Hello, Scripting Guy! You've described how to search for a single word or phrase in a text file, but how do you search for two phrases in a text file? I want to know if the file contains Windows 2000 or Windows XP.
--JR
For:
Hello, JR. You know, it's hard enough for the Scripting Guys to do one thing, so it's almost impossible for them to do two things. But what we're going to tell you is this: as long as you don't mind if we introduce you to an easy way to search for multiple items in a text file, we'll show you how to search for multiple items in a text file.
Attention. Why do you call it a "simple method"? We're not going to bother setting up an array or some other complex framework for multiple searches. Instead, we're going to search for the first term when we first search for the file, and then we'll search for the second term when we search the file for the second time. This method is not very good, but it is very simple, and very effective.
Here's a short, simple script that tells you whether you can find the term Windows 2000 or Windows XP in a text file C:\Scripts\Text.txt:
Copy Code code as follows:
Const ForReading = 1
BlnFound = False
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\Scripts\Test.txt", ForReading)
StrContents = Objfile.readall
Objfile.close
If InStr (strContents, "Windows") Then
BlnFound = True
End If
If InStr (strContents, "Windows XP") Then
BlnFound = True
End If
If BlnFound Then
WScript.Echo "Either Windows or Windows XP appears in this file."
Else
WScript.Echo "Neither Windows nor Windows XP appears in this file."
End If
This script begins by defining a constant named ForReading and setting its value to 1; we'll use it when we open a text file. We also created a variable named BlnFound and specified its value as False; we will use this variable to track whether any one of the search terms is found in the file. If at least one term is found, we will change the value of BlnFound to True, otherwise the value will remain False.
Next we open the file C:\Scripts\Test.txt for reading, and then use the ReadAll method to read all the contents of the file into a variable named strContents, and we will actually search for the "copy" of this file stored in memory. Since we no longer need this physical file, we call the Close method to turn the file off.
At this point, we can do the first search. The following line of code uses the INSTR function to determine whether a string of Windows 2000 can be found at a location in the variable strContents:
If InStr (strContents, "Windows") Then
If InStr is true, we set the value of BlnFound to True, and if InStr is False, we will skip directly to the next search. In the next search, we repeat this process, which will search the string for Windows XP:
If InStr (strContents, "Windows XP") Then
If Windows 2000 or Windows XP (or both) is found, BlnFound will be True, and BlnFound will remain False if neither is found. At the end of the script, we examine the value of the BlnFound and indicate whether one or more of the search phrases were found in the file.
But what if you want to know whether these two search phrases are included in the file at the same time? We will no longer elaborate on this, but the following script tells you whether you can find two target phrases in the file at the same time:
Const ForReading = 1
Intfound = 0
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\Scripts\Test.txt", ForReading)
StrContents = Objfile.readall
Objfile.close
If InStr (strContents, "Windows") Then
Intfound = intfound + 1
End If
If InStr (strContents, "Windows XP") Then
Intfound = intfound + 1
End If
If intfound = 2 Then
WScript.Echo "The text file contains both Windows XP."
Else
WScript.Echo "The text file does not contain both Windows XP."
End If
Yes, the script is really similar to the previous script. The biggest difference is that we don't use the true-false variable; instead, we use a counter variable called Intfound. The script first searches for Windows 2000, and if the phrase is found, the intfound is added to 1. (since Intfound is 0 at the start, this means that intfound will be equal to 1 at this time.) )
The script then searches for Windows XP, and if the phrase is found, the Intfound value is added to 1. What's the end result? At the end of the script, only two target phrases are found at the same time, Intfound equals 2, if Intfound equals 0 or 1, either none is found or only one of the target phrases is found. All you have to do now is echo the search results.