Mega Code Archive

 
Categories / VisualBasic Script / File Path
 

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