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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for files containing certain array strings

Status
Not open for further replies.

KJOYFL

IS-IT--Management
Jun 20, 2011
3
US
Working through a script that will search a file share for files whose names contain strings specified within an array. Also, if it encountered a directory where access is denied, it will skip the directory and continue to the next one.

Files found are then written to a log file. Below is what I have so far. I am having trouble getting it to actually search for the array strings. While I can get it to echo, it isn't actually "searching for the correct context.

'ON ERROR RESUME NEXT

Dim Sizereport
rootfolder = Inputbox("Enter directory/foldername: " & _
chr(10) & chr(10) & "(i.e. C:\Program Files or " & _
"\\Servername\C$\Program Files)" & chr(10) & chr(10), _
"Getfoldersize", "C:\temp")

outputfile = "c:\temp\PasswordFiles_" & Month(now) & Day(now) & Year(now) & ".csv"

Set fso = CreateObject("scripting.filesystemobject")
if fso.fileexists(outputfile) then fso.deletefile(outputfile)


report = ""

arrContent = array("pass","password")

'Run checkfolder
FolderSizeReport = CheckFolder(rootfolder, 0)

Function CheckFolder(objCurrentFolder, SizeReport)
Set oFolder = fso.GetFolder(objCurrentFolder)
For Each oFile In oFolder.files
For Each strLine in arrContent
wscript.echo "Checking" & oFile & " for: " & strLine
If inStr(oFile.Name,arrContent(0)) Then
Report = Report & vbCrLf & oFolder & "\" & oFile.Name & "," & oFile.Size & "," & oFile.DateLastModified
SizeReport = SizeReport + oFile.Size
End If
Next
Next

'Recurse through all of the folders
For Each objNewFolder In oFolder.subFolders
CheckFolder objNewFolder, SizeReport
Next
CheckFolder = SizeReport
End Function

Report = Report & vbCrLf & "Total File Size =" & (FolderSizeReport/1024\1024) & "MB"

Set ts = fso.CreateTextFile (outputfile, ForWriting)
ts.write report
ts.close
 
The InStr() function returns the start position of the search string if it is found (it returns 0 if it is not found). You might try changing the line
Code:
If inStr(oFile.Name,arrContent(0)) Then
to
Code:
If inStr(oFile.Name,arrContent(0)) > 0 Then

Also, the code is only testing for the first value in the array ("pass"); you will also want to test for arrContent(1) ("password"). Consider setting up a for loop to test for each value in the array, if/when you add values to the array the code will still run correctly.
 
Also, the code is only testing for the first value in the array ("pass"); you will also want to test for arrContent(1) ("password"). Consider setting up a for loop to test for each value in the array, if/when you add values to the array the code will still run correctly.

It looks like you already have the correct for..loop but are not making use of it. Change arrContent(0) to strLine.

Code:
If inStr(oFile.Name, [red]strLine[/red]) > 0 Then

-Geates



"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
AWESOME! So, that did it! A combination of the two...changing it to "strLine" and adding a > 0 to the end.

So, strLine helps to process my For...Loop and move through the variable.

What exactly does the "> 0" do?

Also, do I need to add anything to handle folders where I would be access denied? I don't want it to crash when I encounter a folder I may not have permissions to for some reason...

Thanks!
 
Correct, a for..each iterates each item in an object or array.

example
Code:
arrFruit = array("apple", "banana", "pear", "strawberry")
for each strFruit in arrFruit
   msgbox strFruit
next

[green]
output
------
apple
banana
pear
strawberry
[/green]

the following code would do the same thing

Code:
arrFruit = array("apple", "banana", "pear", "strawberry")
for i = 0 to ubound(arrFruit) - 1
   msgbox arrFruit(i)
next


[red]Trap errors[/red] that occur from permission denied events by using On Error Resume Next at the beginning of the function. The [green] > 0[/green] isn't neccessarily required because an if..then evalutes true (non-zero) or false(zero). So if inStr finds the text you are looking for, it will return the position starting at 1, which is a non-zero - true. If inStr does not find the text you are looking for, it will return a 0 - false. However, it is good to include for readability's sake.

Code:
Function CheckFolder(objCurrentFolder, SizeReport)
      [red]on error resume next[/red]      
      Set oFolder = fso.GetFolder(objCurrentFolder)
      For Each oFile In oFolder.files
        For Each strLine in arrContent
            wscript.echo "Checking" & oFile & " for: " & strLine
            If inStr(oFile.Name, strLine) [green]> 0[/green] Then            
    ...
End Function

-Geates

Note: Sometimes I'm not the best at explaining things. Sorry if my explanation is confusing.

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
No, I got it! Ok, so the final thing I want to do is email the results. How would I write the array elements into the objMessage.TextBody section? I want it to automatically populate everything I have into the array into the end of the objMessage section.

For the objMessage.TextBody = "Results from public drive search for files containing the word(s): " I would like it to say word(s): password, pass - OR, whatever I had specified in my array above.

Thanks!

I am using the following:

'Email document when finished
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Public Server Password File Monthly Search Results"
objMessage.From = "test@user.com"
objMessage.To = "test@user.com"
'objMessage.To = "test@user.com"
objMessage.TextBody = "Results from public drive search for files containing the word(s): "
objMessage.AddAttachment "c:\temp\PasswordFiles_" & Month(now) & Day(now) & Year(now) & ".csv"

objMessage.Configuration.Fields.Item(" = 2
objMessage.Configuration.Fields.Item(" = "5.5.5.5"
objMessage.Configuration.Fields.Item(" = 25
objMessage.Configuration.Fields.Update

objMessage.Send
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top