Link to home
Start Free TrialLog in
Avatar of Charles Robinson
Charles RobinsonFlag for United States of America

asked on

Search for Text

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

Avatar of RobSampson
RobSampson
Flag of Australia image

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

Avatar of Charles Robinson

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Rob is an excellent resource!
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