Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to search for two items in a text file with vbscript

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use vbscript to search for two items in the text file, with a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

The 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

The script begins with a constant named ForReading and sets its value to 1; we'll use it when we open the text file. We also created a variable named blnFound and specified its value as False;, which we will use to track whether any of the search terms were 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 the same as False.

Next we open the file C:\ Scripts\ Test.txt to read, and then use the ReadAll method to read all the contents of the file into a variable named strContents; in fact, we will search for the "copy" of this file stored in memory. Since we no longer need this physical file, we call the Close method to close the file.

At this point, we can conduct our first search. The following line of code uses the InStr function to determine whether the string Windows 2000 can be found somewhere 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 will skip directly to the next search. In the next search, we repeat this process, this time searching for the string Windows XP:

If InStr (strContents, "Windows XP") Then

If Windows 2000 or Windows XP (or both) is found, blnFound will be True;. If neither is found, blnFound will still be False. At the end of the script, we check the value of blnFound and indicate whether one or more search phrases were found in the file.

But what if you want to know if both search phrases are included in the file? We will not elaborate on this, but the following script will tell 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 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, this script is indeed very 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; if it finds the phrase, it increments intFound by 1. (since intFound starts with 0, this means that intFound will now be equal to 1. )

The script then searches for Windows XP and, if it finds the phrase, increments the value of intFound by 1. What is the end result? At the end of the script, intFound equals 2 only if two target phrases are found at the same time; if intFound equals 0 or 1, none or only one target phrase is found. All you have to do at this point is to echo the search results.

Thank you for reading this article carefully. I hope the article "how to use vbscript to search for two items in text files" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report