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

Search for File Type Recursively and Count Matches

Status
Not open for further replies.

MZimbaro

IS-IT--Management
Oct 10, 2006
6
US
I have seen several examples of recursive searching, but nothing that will sum at a specific level. For example, My server has all Mail stored on E: drive in the Users main folder (E:\users). Within Users, I have User1, User2, etc. I want to be able to search within each user folder for files ending in .msg so I can track how many messages each user has. Within each User folder, there can be many levels of sub folders.
 
If you Dim a variable in the code before you start your search and initialize it to zero (I know, VBScript initializes it anyway, just a good habit to get into in general), that makes it visible globally. Then every time the particular message type is found you can increment the variable and view the cumulative value in any Sub you want. I do this with backup scripts I've written that only copy the updated files.

Lee
 
I think maybe I have the counters in the wrong place. It's hard to track when the script is moving to a new user. This is how I am doing the recursive search. Maybe somebody can spot the error with my counter placement. I have some extra echo's for debugging purposes.

''''''''''''''''''''''''''''''''''''''
' Copy of Script found on this site
''''''''''''''''''''''''''''''''''''''
Dim FSO, WSH, objDirectory, objFile, TheFiles
Dim msgcount, sumcount

Set FSO = CreateObject("Scripting.FileSystemObject")
Set WSH = CreateObject("Wscript.Shell")


Set objDirectory = FSO.GetFolder("e:\users")
Set TheFiles = objDirectory.Files

Parentfolder = mid(fso.GetFolder(objDirectory), &_
InstrRev(fso.GetFolder(objDirectory),"\",-1,0)+1, &_
len(fso.GetFolder(objDirectory)) - InstrRev &_
(fso.GetFolder(objDirectory),"\",-1,0)) &_
objextension = "msg"

WorkWithSubFolders objDirectory

'''''''''''''''''''''''''''''''''''''

Function WorkWithSubFolders(objDirectory)
Dim MoreFolders, TempFolder
if (msgcount > 0) Then
sumcount = msgcount
wscript.echo "Count: " & sumcount

end if

msgcount = 0
msgcount = msgcount + ListFilesWithExtension &_(objDirectory)
Set MoreFolders = objDirectory.SubFolders

For Each TempFolder In MoreFolders
wscript.echo "Folder: " & TempFolder
WorkWithSubFolders(TempFolder)
Next
End Function

'''''''''''''''''''''''''''''

Function ListFilesWithExtension(objDirectory)
Dim TheFiles
counter = 0
Set TheFiles = objDirectory.Files
For Each objFile in TheFiles
strExt = fso.GetExtensionName(objFile.Path)
If (strExt = objextension) Then
counter = counter + 1
end if
Next
If (counter > 0 ) Then
ListFilesWithExtension = counter
END IF
End Function

 
I think I found my issue. It had to do with the Function WorkWithSubFolders calling itself. This made it difficult to determine when it was on the next user. So what I did was copy that function and renamed it. Then I call the original function if there are more subfolders.
 
By the way, thanks trollacious, I didn't have the variable set to global until you mentioned it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top