Solved

Search for Text

Posted on 2011-03-15
6
220 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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