How Can I Search for Two Items in a Text File?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! You’ve shown us how to search a text file for a single word or phrase, but how can I search a text file for two phrases? I need to know if a file contains either Windows 2000 or Windows XP.

— JR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JR. You know, it’s hard enough to get the Scripting Guys to do one thing; getting them to do two things would seem to be well-nigh impossible. But we’ll tell you what: as long as you don’t mind us showing you an easy way to search for multiple items in a text file then we’ll show you how to search for multiple items in a text file.

Note. Why is this the “easy way?” Well, we’re not going to bother setting up an array or some other complicated framework for carrying out multiple searches. Instead we’re going to search the file once for the first term, then search it a second time for the second term. It’s not elegant, but it’s easy, and it works.

Here’s a simple little script that can tell you if either the term Windows 2000 or the term Windows XP can be found in the text file C:\Scripts\Text.txt:

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 2000”) Then blnFound = True End If

If InStr(strContents, “Windows XP”) Then blnFound = True End If

If blnFound Then Wscript.Echo “Either Windows 2000 or Windows XP appears in this file.” Else Wscript.Echo “Neither Windows 2000 nor Windows XP appears in this file.” End If

This script begins by defining a constant named ForReading and setting the value to 1; we’ll need to use this when we open our text file. We also create a variable named blnFound and assign it the value False; we’ll use this to keep track of whether or not either of our search terms are found in the file. If we find at least one of the terms we’ll change the value of blnFound to True; otherwise, the value of blnFound will remain False.

Next we open the file C:\Scripts\Test.txt for reading, then use the ReadAll method to read the entire contents of the file into a variable named strContents; we’ll actually conduct our searches on this “copy” of the file stored in memory. Because we don’t need the physical file any more, we call the Close method to close the file.

Now we’re ready for our first search. This line of code uses the InStr function to determine whether the string Windows 2000 can be found anywhere in the variable strContents:

If InStr(strContents, “Windows 2000”) Then

If InStr is True, then we set the value of blnFound to True; if InStr is False we simply skip to the next search. In that next search we repeat the process, this time searching for the string Windows XP:

If InStr(strContents, “Windows XP”) Then

If we found either Windows 2000 or Windows XP (or both), blnFound will beTrue; if we didn’t find either phrase, then blnFound will still be False. At the end of the script we check the value of blnFound, and indicate whether or not one or more of the search phrases were found in the file.

But what if you need to know if the file contains both of the search phrases? We won’t explain this is any real detail, but here’s a script that will tell you whether or not both of the target phrases can be found in the file:

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 2000”) 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 2000 and Windows XP.” Else Wscript.Echo “The text file does not contain both Windows 2000 and Windows XP.” End If

Yes, it is similar to our previous script. The big difference is that we don’t use a True-False variable; instead we use a counter variable named intFound. The script first searches for Windows 2000; if the phrase is found then intFound is incremented by 1. (Because intFound starts off at 0, that means intFound would now be equal to 1.)

The script then searches for Windows XP and, if the phrase is found, increments the value of intFound by 1. The net result? At the end of the script, intFound will equal 2 only if both target phrases were found; if intFound equals 0 or 1, then either neither target phrase or only one target phrase was found. All that’s left at that point is to echo back the results of our search.

0 comments

Discussion is closed.

Feedback usabilla icon