Use vbscript to search for two items in a text file

Source: Internet
Author: User

Q:
Hello, script expert! You have introduced how to search for a single word or phrase in a text file, but how to search for two phrases in a text file? I want to know if the file contains Windows 2000 or Windows XP.
-- JR
A:
Hello, JR. You know, it is difficult for the scripting experts to do one thing. It is almost impossible for them to do two things. But what we want to tell you is: as long as you don't mind, we will introduce you to a simple method to search for multiple items in a text file, we will show you how to search for multiple items in a text file.
Note. Why is it a "Simple Method? We do not intend to bother setting up arrays or other complex frameworks for multiple searches. Instead, we plan to search for the first term when searching for a file for the first time, and then for the second term when searching for a file for the second time. Although this method is not very good, it is very simple and effective.
The following is a simple script that tells you whether the term Windows 2000 or Windows XP can be found in the text file C: \ Scripts \ Text.txt: Copy codeThe Code is 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 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 starts with a constant named ForReading and sets its value to 1. We will use it when opening a text file. We also created a variable named blnFound and set its value to False. We will use this variable to track whether any search term 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 unchanged.
Next, open the file C: \ Scripts \ Test.txt to read the file, and then use the ReadAll method to read all the content of the file into the variable named strContents; in fact, we will search for the "copy" of the file stored in the memory. Because we no longer need this physical file, we call the Close method to Close the file.
Now, we can perform the first search. The following code uses the InStr function to determine whether the string Windows 2000 can be found at a location in the variable strContents:
If InStr (strContents, "Windows 2000") Then
If InStr is True, we set the value of blnFound to True. If InStr is False, we will jump directly to the next search. In the next search, we repeat this process, this time we will search for the string Windows XP:
If InStr (strContents, "Windows XP") Then
If Windows 2000 or Windows XP is found (or both), blnFound is True; if neither is found, blnFound is still False. At the end of the script, we check the blnFound value and indicate whether one or more search phrases are found in the file.
But what if you want to know whether the file contains both search phrases? We will not elaborate on this, but the following script tells you whether two target phrases can be found simultaneously 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, the script is indeed similar to the previous script. The biggest difference is that we didn't use the True-False variable; instead, we used a counter variable named intFound. This script first searches for Windows 2000. If this phrase is found, intFound is added to 1. (Since intFound starts with 0, this means that intFound will be equal to 1 at this time .)
The script then searches for Windows XP. If this phrase is found, the intFound value is added to 1. What is the final result? At the end of the script, intFound is equal to 2 only when two target phrases are found at the same time. If intFound is equal to 0 or 1, it indicates that one target phrase is not found or only one target phrase is found. In this case, the search result is displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.