Search for a string in today, yesterday or the day before's files

Thursday Jul 1st 2004 by DatabaseJournal.com Staff

This VBScript allows you to search files that are a few days old, in a folder for a keyword.

>>Script Language and Platform: VBScript, SQL Server
This VBScript allows you to search files that are a few days old, in a folder for a keyword. It is useful to search files like archived log files in the same folder so that you can look in to only those log files which have the keywords.


'This will search todays and yesterdays files for a particular string.
cscript SearchLog.vbs "\\Servername\d$\crondit\apps\robocopy\logs\" "enough" "1"

'This will search today, yesterday, day before yesterday files 

cscript SearchLog.vbs "C:\Winnt\syslogs\" "Error" "2"

Author: MAK

'Author: MAK
'Objective: To search a string in all the files (2 days old) in a folder
'contact: mak_999@yahoo.com
'Date written: April 5, 2003
Set objArgs = WScript.Arguments
Const ForReading = 1 
Const ForWriting = 2 
set WshShell = WScript.CreateObject("WScript.Shell")
'strStartup = "\\server1\d$\crondit\apps\robocopy\logs\"
strStartup = objArgs(0)
'SearchString= "enough"
SearchString= objArgs(1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set fso2 = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strStartup)
for each x in f.files
	if left(x.datelastmodified,10) >= left(date-noofdays,10) then
		filenamepath=strStartup + x.name
		Set ReadFile = fso2.OpenTextFile(filenamepath, ForReading, TristateFalse) 
		thisTXT =	ReadFile.ReadAll 

		ValueSearch = InStr(1,thisTXT,SearchString,1) 
			If ValueSearch>=1 Then 
				msgbox  "Found '"+ Searchstring + "' in "+ filenamepath
			end if
	end if
msgbox "Completed"

Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued

Back to Database Journal Home

Mobile Site | Full Site