Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching files by modified date. 4

Status
Not open for further replies.

ponoodle

IS-IT--Management
Dec 10, 2010
135
US
I'm trying to fix some scripts that someone else wrote before I worked here. We want to search all files on specified computers for a specified string, but we only want to search files that have been modified within a certain amount of days. There are two scripts involved.

First Script
Code:
on error resume next
Const ForReading = 1
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
srcTXT = "C:\scripts\accessed.txt"
Set objFileSrc = objFSO.OpenTextFile(srcTXT, ForReading)

daysOld = InputBox("How many days ago was the file modified?")
SearchString = InputBox("What is the string that we are searching for?")


i = 0
Do Until objFileSrc.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFileSrc.ReadLine
     i = i + 1
Loop

if i = 0 then
	wscript.quit
end if

objFileSrc.Close


For x = 0 to Ubound(arrFileLines) 
		objShell.Run "cscript ""C:\scripts\findtribal.vbs"" " & arrFileLines(x) & " " & daysOld & " " & SearchString
Next
Second Script
Code:
on error resume next

Dim ArgObj
Set ArgObj = WScript.Arguments

Const FOR_READING = 1
Const ForReading = 1

strFolder = "\\" & ArgObj(0) & "\C$"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
Set colFiles = objFolder.Files

Set fso2 = CreateObject("Scripting.FileSystemObject")

noofdays = ArgObj(1)

SearchString = ArgObj(2)
wscript.echo "Searching " & ArgObj(0) & " for '" & SearchString & "' within files modified " & noofdays & " days ago."

For Each objFile In colFiles
		filenamepath = objFile.Path
		
		Set ReadFile = fso2.OpenTextFile(filenamepath, ForReading, TristateFalse) 
		thisTXT =	ReadFile.ReadAll 

		ValueSearch = InStr(1,thisTXT,SearchString,1) 
			If ValueSearch>=1 Then 
				wscript.echo  "Found '"+ Searchstring + "' in "+ objFile.Path
				Wscript.Sleep 5000
			end if
Next
ShowSubFolders(objFolder)


Sub ShowSubFolders(objFolder)
on error resume next
Set colFolders = objFolder.SubFolders

For Each objSubFolder In colFolders
	If objSubFolder.Path <> strFolder & "\System Volume Information" then

		Set colFiles = objSubFolder.Files

		
		For Each objFile In colFiles
			'if trim(left(objFile.datelastmodified-nofdays,10)) = trim(left(date-noofdays,10)) then
			IF Date - DateValue(objFile.datelastmodified) < 15 THEN 
			filenamepath = objFile.Path
				Set ReadFile = fso2.OpenTextFile(filenamepath, ForReading, TristateFalse) 
				thisTXT =	ReadFile.ReadAll 
				ValueSearch = InStr(1,thisTXT,SearchString,1) 
				If ValueSearch>=1 Then 
					wscript.echo "Found '"+ Searchstring + "' in "+ objFile.Path
					Wscript.Sleep 5000
				end if
			end if
		Next
		ShowSubFolders(objSubFolder)
	end if
Next
End Sub


WScript.Echo objFile.Path
WScript.Echo objFile.Name
wscript.echo "completed"
Wscript.Sleep 999999999

Thanks for any help.
 
Yea i missed that... The loop below should work

Code:
For x = 0 to Ubound(arrFileLines)
    Wscript.echo "Searching for '" & SearchString & "' within files modified " & noofdays & " days ago."
    ShowSubFolders([COLOR=blue]objFSO.GetFolder("\\" & arrFileLines(x) & "\c$")[/color])
Next
 
For x = 0 to Ubound(arrFileLines)
Wscript.echo "Searching for '" & SearchString & "' within files modified " & noofdays & " days ago."
ShowSubFolders(objFSO.GetFolder("\\" & arrFileLines(x) & "\c$"))

i noticed that you have ' followed by " after searching for and then " followed by '

shouldnt it be:
Wscript.echo "Searching for: " & SearchString & " within files modified: " & noofdays & " days ago."

? just a question for myself i guess if i am wrong here id like to know why it would allow for the single ' ?

is that the way you quote the results of the search strings?

Devon
Intel Corp. (HF)
 
He's just putting single quotes around the search string in the echo output. The output will look something like this:
Code:
Searching for: 'SomeSearchString' within files modified: 2 days ago.
 
It runs fine now, except it still finds files that are several years old when I search for files that are only a few days old.
 
Hadn't noticed that... It is interpreting noofdays as a string, converting to an integer fixes that:

Code:
If Date - DateValue(objFile.datelastmodified) < [COLOR=blue][b]CLng([/b][/color]noofdays[COLOR=blue][b])[/b][/color] Then
 
Yes!, that worked. Thank you very much for your help. Can I give more than 1 star? =)

I just found this site while trying to solve this problem. It's nice here, I think I'll stay a while.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top