問: 您好,腳本專家! 您曾經介紹過如何在文本文件中搜索單個詞或短語,但如何在文本文件中搜索兩個短語呢? 我想知道文件中是否包含 Windows 2000 或 Windows XP。 -- JR 答: 您好,JR。您知道,讓腳本專家去做一件事情就已經夠困難的了;可見讓他們去做兩件事情幾乎是不可能的。 但我們要告訴您的是: 只要您不介意我們向您介紹一種簡單的方法來在文本文件中搜索多個項,我們就會向您介紹如何在文本文件中搜索多個項。 注意。 為什么稱之為“簡單的方法”呢? 我們不打算費神設置數組或其他某個復雜的框架來進行多個搜索。 相反,我們打算在第一次搜索文件時搜索第一個術語,而后在第二次搜索文件時搜索第二個術語。 這種方法雖不是很好,但卻很簡單,且很有效。 以下是一小段簡單的腳本,它可告訴您是否可在文本文件 C:/Scripts/Text.txt 中找到術語 Windows 2000 或 Windows XP:
復制代碼 代碼如下:
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