Explanation Needed - How Code that Recursively Search Works (1 Viewer)

llyal

Registered User.
Local time
Today, 17:59
Joined
Feb 1, 2000
Messages
72
Explanation Needed - How Code that Recursively Search Works

I have a function that uses the FSO to recursively search folders looking for files. The code is included at the bottom of the posts (mind the typos!) I can use this function and modify it to suit my needs, but I do not have a full understanding how the function works. I understand the comments in the function (which describe what the function is doing), but I cannot grasp how the function can call itself to do a recursive search. Is this what one calls a "call back" function? Can someone explain the recursive behavior of this function step-by-step? for example, what is going on when this line executes:

funFindFile = funFindFile + funFindFile(fldSubFolder.Path, strFilePattern, intFolderCount, intFileCount)

I interpret this code as: the return of the function = the return of the function + return of the function. To me, this does not make any sense- but, it works! how is this so?

Thank you!

--Llyal

Private Function funFindFile(ByVal strFolderPathname As String, strFilePattern As String, intFolderCount As Integer, intFileCount As Integer) As Long

Dim objFSO As New FileSystemObject
Dim fldFolder as Folder
Dim fldSubFolder As Folder
Dim strFileName as String

' set FSO to folder specified by strFolderPathname
Set fldFolder = objFSO.GetFolder(strFolderPathname)

' get file in folder that matches strFilePattern
strFileName = Dir(objFSO.BuildPath(fldFolder.Path, strFilePattern), vbNormal Or vbHidden Or vbSystem Or vbReadOnly)

' if file exists
While Len(strFileName) <> 0
' get complete pathname for matched file
funFindFile = funFindFile + FileLen(objFSO.BuildPath(fldFolder.Path,, strFileName))

' increment file count
intFileCount = intFileCount + 1

' add matched file pathname to files found listbox
lstFoundFiles.AddItem objFSO.BuildPath(fldFolder.Path, strFileName)

' get next file
strFileName = Dir()

' yield control back to user
DoEvents
Wend

lblSearchPathname = "Searching " & vbCrLf & fldFolder.Path & "\..."
intFolderCount = intFolderCount + 1

If (fldFolder.SubFolders.Count > 0) Then
For Each fldSubFolder In fldFolder.SubFolders
DoEvents
funFindFile = funFindFile + funFindFile(fldSubFolder.Path, strFilePattern, intFolderCount, intFileCount)
Next
End If

End Function
 

AlanS

Registered User.
Local time
Today, 12:59
Joined
Mar 23, 2001
Messages
292
I don't completely understand your routine (perhaps because I work in Access 97, which does not seem to include FSO), but I think I can help with a few things. Normally, when a function name appears on the right side of an assignment statement within the function itself, that does result in a recursive call to the function. But I think in your case, since the name appears there both with and without the argument list, VBA may interpret the reference without the argument list as simply the current value of the function, i.e., what the function would return if an Exit Function were executed at that point.

I think what the code is doing is gathering all the matching file names within a single folder, and then calling itself recursively to append the file names from other folders.

Hope this helps.


[This message has been edited by AlanS (edited 12-07-2001).]
 

Users who are viewing this thread

Top Bottom