Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search for Text

Posted on 2011-03-15
6
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

704 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