Results 1 to 2 of 2
  1. #1
    Star Lounger
    Join Date
    Jan 2001
    Thanked 0 Times in 0 Posts

    Explanation Needed - How Code that Recursively Sea (VB/VBA)

    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 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 recursively 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?

    Thanks you!


    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

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

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

    End Function

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Burwash, East Sussex, United Kingdom
    Thanked 229 Times in 210 Posts

    Re: Explanation Needed - How Code that Recursively Sea (VB/VBA)

    It might make a little more sense if you think of it as:
    the new value of the function = the old value + the value of the function applied to these new arguments.
    The reason for the recursion (as I understand it) is to allow the function to loop through all the subfolders. So you pass it a folder name and a file pattern, it checks that folder for matches, and also checks for subfolders. If there are any subfolders, the function goes through the same procedure for each of them. This allows you to go through all the levels of subfolders without needing to know in advance how many there are.
    The following example from Excel's Help is probably a little easier to understand but it shows the same principle:
    <pre>Function Factorial (N)
    If N <= 1 Then ' Reached end of recursive calls.
    Factorial = 1 ' (N = 0) so climb back out of calls.
    Else ' Call Factorial again if N > 0.
    Factorial = Factorial(N - 1) * N
    End If
    End Function

    Does that make any sense?

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts