Solved

Search for Text

Posted on 2011-03-15
6
226 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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