Search For String in Multiple Text Files

By | November 17, 2006

I needed a script that could search thru log files and fing an email address.
This script can do just that! I used some code I found on the Internet and
modified if for my purposes! I hope you like this script.

———-COPY EVERYTHING BELOW THIS LINE———-

'Script to find a specified string in all text documents/logs (e.g.,file.txt, file.log)
'The script will search inside of all of the text documnets in a folder for a specified string.
'Created by Cheyenne Harden 11.17.2006

Option Explicit
   'Constants
    Const ForWriting = 8
   
   'Vars
    Dim strGFI, objFSO1, objOutputFile
    Dim strOTF, MyString, objFileSystem
    Dim strWSE, strWritePath, objFile
 Dim strInput, cFOL, StrDirectory
 
   'Inputboxes
   cFOL = InputBox("Enter the path to search"& VbCrLf & "(e.g., C:\temp)")
   strInput = Inputbox("Enter the text you would like to search for.")
  
   'Path and file name
   MyString = "FileSearch"
   strWritePath = "c:\scripts" & MyString & ".txt"
   strDirectory = "C:\scripts\"
  
   Set objFSO1 = CreateObject("Scripting.FileSystemObject")

If objFSO1.FileExists(strWritePath) Then
 'Wscript.Echo "The file Exists"
Else
 Set objFile = objFSO1.CreateTextFile(strDirectory & MyString & ".txt")
 objFile = ""

End If
  
   Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set objOutputFile = objFileSystem.OpenTextFile(strDirectory & MyString & ".txt", ForWriting)

If cFOL > "" Then
 If strInput > "" Then
 
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objGFO
    Set objGFO = objFSO.GetFolder(cFOL)
    Dim objGFI
    Set objGFI = objGFO.Files
    For Each strGFI in objGFI
        Dim objOTF
        Set objOTF = objFSO.OpenTextFile(cFOL & "\" & strGFI.Name,1)
        Do While Not objOTF.AtEndOfStream
            strOTF = objOTF.ReadAll()
   
        Loop
            objOTF.Close()
        Set objOTF = Nothing
       '*
        If  InStr(LCase(strOTF), strInput) > 0 Then
            strWSE = strWSE & strGFI.Name & " contains " & strInput & _
   vbCrLf
   
    
        Else
            strWSE = strWSE & strGFI.Name & " Does not contain: " & strInput & vbCrLf
        End If
  
    Next
 objOutputFile.WriteLine(strWSE)
 objOutputFile.Close
 
   '*
    Set objGFI = Nothing
    Set objGFO = Nothing
    Set objFSO = Nothing
 Set objFileSystem = Nothing
 Set objFSO1 = Nothing
 
   '*
    WScript.Echo strWSE
  Else
  Wscript.Echo "You Clicked Cancel or no search string was defined."
  End If
Else
Wscript.Echo "You Clicked Cancel or no search path was defined."
WScript.Quit
End If

———-COPY EVERYTHING ABOVE THIS LINE———-

PLEASE MAKE SURE NO WORD WRAPPING IS HAPPENING IN YOUR SCRIPT!!!

To make this script work you will need one things!

1. You will need to have Admin privlidges on the computer.
2. You must have a c:\scripts folder
3. UNC paths do work (e.g., \\server01\c$\temp)
4. This script can read plain text like: .log, .txt, .rtf. It cannot read .doc and such!

This information is provided "AS IS" with no warranties expressed or implied.

Leave a Reply

Your email address will not be published. Required fields are marked *