I want to search several words at the same time in a text file.
For example think I want to search these 3 words: Majid,superuser,device
Normally I should search for them one by one, and I can not search all of them at the same time. So I want to search these words at the same time in a text file.
I want to enter those 3 words in a text file, one word per line. Let's name it SearchText. Now I have a Target Text which I want to search those words in it. Let's name it TargetText.
I want to tell an app or something similar to get words from SearchText and find them in TargetText and Highlights them or gives me the find result.
I hope I'm clear. So can anyone hep me?
You're clear. I think the best option would be Regex.
Try this:
Option Explicit
Dim oFso : Set oFso = CreateObject("Scripting.FileSystemObject")
Dim srcPath : SrcPath = oFso.GetParentFolderName(WScript.ScriptFullName)
Dim sWordList : sWordList = ofso.OpenTextFile(oFso.BuildPath(srcPath, "search.txt")).ReadAll()
Dim sTargFile : sTargFile = ofso.OpenTextFile(oFso.BuildPath(srcPath, "target.txt")).ReadAll()
Dim strWords : strWords = Join(Split(sWordList, vbCrLf), "|")
Dim oReg : Set oReg = New RegExp
Dim oDict : Set oDict = CreateObject("Scripting.Dictionary") 'for found words
oDict.CompareMode = vbTextCompare 'case insensitive
With oReg
.Global = True
.IgnoreCase = True
.Pattern = "("& strWords &")" ' (word1|word2|word3|etc..)
'if the words contain some special chars for regex, you may need to escape with \ char
'see the information below: http://goo.gl/cqGVp
Dim collFND : Set collFND = oReg.Execute(sTargFile)
Dim Fnd
'WScript.Echo String(50, "-")
For Each Fnd In collFND
With Fnd
'WScript.Echo "Word : "& .Value, ", Position: ("& .FirstIndex &","& .Length &")"
oDict(.Value) = ""
End With
Next
Set collFND = Nothing
'WScript.Echo String(50, "-"), vbCrLf, "Higlighted Output:", oReg.Replace(sTargFile, "[$1]")
ofso.CreateTextFile(oFso.BuildPath(srcPath, "foundwords.txt"),True).Write(Join(oDict.Keys, vbCrLf)) 'found words saved
End With