Recursive Search using the FileSystemObject Model : FileSystemObject « File Path « VBA / Excel / Access / Word






Recursive Search using the FileSystemObject Model

 
Private Sub cmdStartSearch_Click()
    Call FindFile("yourFile", "C:\", False)
    Unload frmSearch
End Sub

Private Sub FindFile(target As String, ByVal aPath As String, foundTarget As Boolean)
    Dim myFileSystemObject As FileSystemObject, curFolder As folder, folder As folder
    Dim folColl As Folders, file As file, fileColl As Files
    Set myFileSystemObject = New FileSystemObject
    Set curFolder = myFileSystemObject.GetFolder(aPath)
    Set folderList = curFolder.SubFolders
    Set fileList = curFolder.Files

    For Each file In fileList
        If file.name = target Then
            foundTarget = True
            frmFileSystem.lblPath.Caption = file.Path
            Exit Sub
        End If
    Next

    If Not foundTarget Then
        For Each folder In folderList
            DoEvents        'Yield execution so other events may be processed
            If Not foundTarget Then
                 FindFile target, folder.Path, foundTarget
            End If
        Next
    End If
    Set myFileSystemObject = Nothing
    Set curFolder = Nothing
    Set folderList = Nothing
    Set fileList = Nothing
End Sub

 








Related examples in the same category

1.Copying a File
2.Display selected file information
3.Delete file by using FileSystemObject
4.Delete Folder by using FileSystemObject
5.Listing files with a For...Each loop