johnpayback
IS-IT--Management
I would like to know how I can search a text file with multiple keywords. I would like it to find lines in the text file that contain all of the keywords. So, an all or nothing. If you know how to make vbscript do this please let me know. An example would be great. I've been trying it using the grep command kind of like below but it only pulls the last keyword rather than lines with all keywords. I know GREP is not the way to go but for really large files it seemed to be much quicker.
The example above works except that it only returns the lines with the last keyword entered. Also...I didn't put it above but I do ask for user input for the keywords.
I do not believe grep is the best way to do this but for really large files it seems to work very fast whereas when I tried regexp it would take up to 10 minutes to pull the data.
JP
Code:
dim str1, str2, str3, str4, str5, str6
dim keyword1, keyword2, keyword3, keyword4, keyword5, keyword6
str1 = "grep -i " & Chr(34) keyword1 Chr(34)
str2 = " | grep -i " & Chr(34) keyword2 Chr(34)
str3 = " | grep -i " & Chr(34) keyword3 Chr(34)
str4 = " | grep -i " & Chr(34) keyword4 Chr(34)
str5 = " | grep -i " & Chr(34) keyword5 Chr(34)
str6 = " | grep -i " & Chr(34) keyword6 Chr(34)
strgrep = str1 & str2 & str3 & str4 & str5 & str6
FolderToSearch = "C:\pathto\folderto\filename.txt"
strcmd = "%comspec% /Q /C " & strgrep & " " & FolderToSearch
set oexec=oWSH.exec(strcmd)
do while not oexec.StdOut.AtEndOfStream
thing = oexec.StdOut.ReadAll
strPResult = Replace(thing,vblf,"Chr(13)")
wscript.echo "Chr(13)" & strPResult
wscript.flush
loop
The example above works except that it only returns the lines with the last keyword entered. Also...I didn't put it above but I do ask for user input for the keywords.
I do not believe grep is the best way to do this but for really large files it seems to work very fast whereas when I tried regexp it would take up to 10 minutes to pull the data.
JP