Solved

Search for Text

Posted on 2011-03-15
6
216 Views
Last Modified: 2012-05-11
I have a script that I borrowed from a gentlemen on this forum. The script works beautifully I would like to add some functionality to it. 1) I would like to search multiple folders, these folders are constant so once programmed, there will be changes.
2) I would like to use an input box for "Text_To_Find"
3) Instead of creating a text report, I would like to display to screen with an option to save a report.

I understand the script as it stands, I just do not know how to add the remainder.
'Change the next three lines'
Const FOLDER_TO_SEARCH = ""
Const FILES_TO_FIND = "*.csv"
Const TEXT_TO_FIND = ""
Dim objFSO, objFolder, objFile, objTextStream, objReport, objRegex, colMatches, strRoot, strExtension, varBuffer, arrFTF
Dim lngFilesChecked, lngFilesMatched, lngHits
arrFTF = Split(FILES_TO_FIND, ".")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the output file name on the next line'
Set objReport = objFSO.CreateTextFile("C:\Temp2\FileSearch.txt")
objReport.WriteLine "Folder to Search: " & FOLDER_TO_SEARCH
objReport.WriteLine "Files to Search: " & FILES_TO_FIND
objReport.WriteLine "Text to Find: " & TEXT_TO_FIND
objReport.WriteLine ""
Set objRegex = CreateObject("VBscript.RegExp")
With objRegex
    .IgnoreCase = True
    .Pattern = TEXT_TO_FIND
    .Global = True
End With
Set objFolder = objFSO.GetFolder(FOLDER_TO_SEARCH)
For Each objFile In objFolder.Files
    strRoot = objFSO.GetBaseName(objFile.Name)
    strExtension = objFSO.GetExtensionName(objFile.Name)
    If (strRoot = arrFTF(0)) Or (arrFTF(0) = "*") Then
        If (strExtension = arrFTF(1)) Or (arrFTF(1) = "*") Then
            lngFilesChecked = lngFilesChecked + 1
            Set objTextStream = objFSO.OpenTextFile(objFile.Path)
            varBuffer = objTextStream.ReadAll
            Set colMatches = objRegex.Execute(varBuffer)
            If colMatches.count > 0 Then
                lngFilesMatched = lngFilesMatched + 1
                lngHits = lngHits + colMatches.count
                objReport.WriteLine "File: " & objFile.Path & "    Matches: " & colMatches.count
            End If
            objTextStream.Close
        End If
    End If
Next
objReport.WriteLine ""
objReport.WriteLine "Files Checked: " & lngFilesChecked
objReport.WriteLine "Files with Text: " & lngFilesMatched
objReport.WriteLine "Total Matches: " & lngHits
objReport.Close
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objTextStream = Nothing
Set objReport = Nothing
Set objRegex = Nothing
Set colMatches = Nothing
WScript.Echo "Search Complete"
WScript.Quit

Open in new window

0
Comment
Question by:robby6161
  • 3
  • 3
6 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35143464
Hi, this will allow you to specify multiple folders in the FOLDER_TO_SEARCH array. and it will also provide an input box for TEXT_TO_FIND.  What kind of output do you want?  Just what's in the report to a DOS box?

Regards,

Rob.
'Change the next three lines'
Const FILES_TO_FIND = "*.csv"
FOLDER_TO_SEARCH = Array("C:\Temp1", "C:\Temp2")
TEXT_TO_FIND = InputBox("Enter the text to find:", "Text to find")
Dim objFSO, objFolder, objFile, objTextStream, objReport, objRegex, colMatches, strRoot, strExtension, varBuffer, arrFTF
Dim lngFilesChecked, lngFilesMatched, lngHits
arrFTF = Split(FILES_TO_FIND, ".")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the output file name on the next line'
Set objReport = objFSO.CreateTextFile("C:\Temp2\FileSearch.txt")
objReport.WriteLine "Folder to Search: " & FOLDER_TO_SEARCH
objReport.WriteLine "Files to Search: " & FILES_TO_FIND
objReport.WriteLine "Text to Find: " & TEXT_TO_FIND
objReport.WriteLine ""
Set objRegex = CreateObject("VBscript.RegExp")
With objRegex
    .IgnoreCase = True
    .Pattern = TEXT_TO_FIND
    .Global = True
End With
For Each strFolderPath In FOLDER_TO_SEARCH
	Set objFolder = objFSO.GetFolder(strFolderPath)
	For Each objFile In objFolder.Files
	    strRoot = objFSO.GetBaseName(objFile.Name)
	    strExtension = objFSO.GetExtensionName(objFile.Name)
	    If (strRoot = arrFTF(0)) Or (arrFTF(0) = "*") Then
	        If (strExtension = arrFTF(1)) Or (arrFTF(1) = "*") Then
	            lngFilesChecked = lngFilesChecked + 1
	            Set objTextStream = objFSO.OpenTextFile(objFile.Path)
	            varBuffer = objTextStream.ReadAll
	            Set colMatches = objRegex.Execute(varBuffer)
	            If colMatches.count > 0 Then
	                lngFilesMatched = lngFilesMatched + 1
	                lngHits = lngHits + colMatches.count
	                objReport.WriteLine "File: " & objFile.Path & "    Matches: " & colMatches.count
	            End If
	            objTextStream.Close
	        End If
	    End If
	Next
Next
objReport.WriteLine ""
objReport.WriteLine "Files Checked: " & lngFilesChecked
objReport.WriteLine "Files with Text: " & lngFilesMatched
objReport.WriteLine "Total Matches: " & lngHits
objReport.Close
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
Set objTextStream = Nothing
Set objReport = Nothing
Set objRegex = Nothing
Set colMatches = Nothing
WScript.Echo "Search Complete"
WScript.Quit

Open in new window

0
 

Author Comment

by:robby6161
ID: 35152140
Thank you for the code. It errors out at line 11 (Microsoft VBScript runtime error: Type mismatch).

To answer your question I would like to have it open Notepad and display the information, then I could save the document from there.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 250 total points
ID: 35152333
OK, I fixed the error.  It will now display in notepad, but it has to write to *some* file for that to happen.  So, it writes to TemporaryFileSearchResults.txt in the same folder as the script, does the search, then opens the report in Notepad for you to save elsewhere.  When Notepad is closed, the temporary file is deleted.

Regards,

Rob.
'Change the next three lines'
Const FILES_TO_FIND = "*.csv"
strReportFile = "TemporaryFileSearchResults.txt"
FOLDER_TO_SEARCH = Array("C:\Temp1", "C:\Temp2")
TEXT_TO_FIND = InputBox("Enter the text to find:", "Text to find")
Dim objFSO, objFolder, objFile, objTextStream, objReport, objRegex, colMatches, strRoot, strExtension, varBuffer, arrFTF
Dim lngFilesChecked, lngFilesMatched, lngHits
arrFTF = Split(FILES_TO_FIND, ".")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the output file name on the next line'
Set objReport = objFSO.CreateTextFile(strReportFile)
objReport.WriteLine ""
objReport.WriteLine "Files to Search: " & FILES_TO_FIND
objReport.WriteLine "Text to Find: " & TEXT_TO_FIND
objReport.WriteLine ""
Set objRegex = CreateObject("VBscript.RegExp")
With objRegex
    .IgnoreCase = True
    .Pattern = TEXT_TO_FIND
    .Global = True
End With
For Each strFolderPath In FOLDER_TO_SEARCH
	objReport.WriteLine VbCrLf & "Folder to Search: " & strFolderPath & VbCrLf
	Set objFolder = objFSO.GetFolder(strFolderPath)
	For Each objFile In objFolder.Files
	    strRoot = objFSO.GetBaseName(objFile.Name)
	    strExtension = objFSO.GetExtensionName(objFile.Name)
	    If (strRoot = arrFTF(0)) Or (arrFTF(0) = "*") Then
	        If (strExtension = arrFTF(1)) Or (arrFTF(1) = "*") Then
	            lngFilesChecked = lngFilesChecked + 1
	            Set objTextStream = objFSO.OpenTextFile(objFile.Path)
	            varBuffer = objTextStream.ReadAll
	            Set colMatches = objRegex.Execute(varBuffer)
	            If colMatches.count > 0 Then
	                lngFilesMatched = lngFilesMatched + 1
	                lngHits = lngHits + colMatches.count
	                objReport.WriteLine "File: " & objFile.Path & "    Matches: " & colMatches.count
	            End If
	            objTextStream.Close
	        End If
	    End If
	Next
Next
objReport.WriteLine ""
objReport.WriteLine "Files Checked: " & lngFilesChecked
objReport.WriteLine "Files with Text: " & lngFilesMatched
objReport.WriteLine "Total Matches: " & lngHits
objReport.Close
Set objFolder = Nothing
Set objFile = Nothing
Set objTextStream = Nothing
Set objReport = Nothing
Set objRegex = Nothing
Set colMatches = Nothing
WScript.Echo "Search Complete. Click OK to see the report. Please save it to a location of your choice."
Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad """ & strReportFile & """", 1, True
objFSO.DeleteFile strReportFile, True
Set objFSO = Nothing
Set objShell = Nothing
WScript.Quit

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:robby6161
ID: 35157862
Rob,
Thank you for the solution. I am going to award you the points.

I have but one more request. The "text_to_find", is found in a record (line), is there a way to bring the record (line) info to the report.  In other words where the info was found I need the whole line of information to show in the report.

Thank you so much for your help.
0
 

Author Closing Comment

by:robby6161
ID: 35157868
Rob is an excellent resource!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35162296
Hi, see if this will get you the line for each match.  It works by looking for <newline><any characters><YOURTEXTHERE><any characters><newline>

so it probably won't work for a match that is at the very first line, or the very last line.......

Regards,

Rob.
'Change the next three lines'
Const FILES_TO_FIND = "*.csv"
strReportFile = "TemporaryFileSearchResults.txt"
FOLDER_TO_SEARCH = Array("C:\Temp1", "C:\Temp2")
TEXT_TO_FIND = InputBox("Enter the text to find:", "Text to find")
Dim objFSO, objFolder, objFile, objTextStream, objReport, objRegex, colMatches, strRoot, strExtension, varBuffer, arrFTF
Dim lngFilesChecked, lngFilesMatched, lngHits
lngFilesMatched = 0
lngHits = 0
arrFTF = Split(FILES_TO_FIND, ".")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Change the output file name on the next line'
Set objReport = objFSO.CreateTextFile(strReportFile)
objReport.WriteLine ""
objReport.WriteLine "Files to Search: " & FILES_TO_FIND
objReport.WriteLine "Text to Find: " & TEXT_TO_FIND
objReport.WriteLine ""
Set objRegex = CreateObject("VBscript.RegExp")
With objRegex
    .IgnoreCase = True
    .Pattern = "[\r\n].*" & TEXT_TO_FIND & ".*[\r\n]"
    .Global = True
End With
For Each strFolderPath In FOLDER_TO_SEARCH
	objReport.WriteLine VbCrLf & "Folder to Search: " & strFolderPath & VbCrLf
	Set objFolder = objFSO.GetFolder(strFolderPath)
	For Each objFile In objFolder.Files
	    strRoot = objFSO.GetBaseName(objFile.Name)
	    strExtension = objFSO.GetExtensionName(objFile.Name)
	    If (strRoot = arrFTF(0)) Or (arrFTF(0) = "*") Then
	        If (strExtension = arrFTF(1)) Or (arrFTF(1) = "*") Then
	            lngFilesChecked = lngFilesChecked + 1
	            Set objTextStream = objFSO.OpenTextFile(objFile.Path)
	            varBuffer = objTextStream.ReadAll
	            Set colMatches = objRegex.Execute(varBuffer)
	            If colMatches.count > 0 Then
	                lngFilesMatched = lngFilesMatched + 1
	                lngHits = lngHits + colMatches.count
	                objReport.WriteLine "File: " & objFile.Path & "    Matches: " & colMatches.count
					For Each objMatch In colMatches
						objReport.WriteLine vbTab & Replace(Replace(Replace(objMatch.Value, vbCr, ""), vbLf, ""), VbCrLf, "")
					Next
	            End If
	            objTextStream.Close
	        End If
	    End If
	Next
Next
objReport.WriteLine ""
objReport.WriteLine "Files Checked: " & lngFilesChecked
objReport.WriteLine "Files with Text: " & lngFilesMatched
objReport.WriteLine "Total Matches: " & lngHits
objReport.Close
Set objFolder = Nothing
Set objFile = Nothing
Set objTextStream = Nothing
Set objReport = Nothing
Set objRegex = Nothing
Set colMatches = Nothing
WScript.Echo "Search Complete. Click OK to see the report. Please save it to a location of your choice."
Set objShell = CreateObject("WScript.Shell")
objShell.Run "notepad """ & strReportFile & """", 1, True
objFSO.DeleteFile strReportFile, True
Set objFSO = Nothing
Set objShell = Nothing
WScript.Quit

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now