'----------------------------------------------------------------------- 'Function: FindFileRecursively 'Finds the first instance of a file within the root folder or one of its subfolders ' 'Remarks: ' Uses recursion ' 'Arguments ' ByVal strRootFolder - As String (absolute folder) ' ByVal strFilename - As String ' 'Returns: ' String with full file pathname based on root folder and file name ' 'Owner: ' 'Date: ' '----------------------------------------------------------------------- Public Function FindFileRecursively(ByVal strRootFolder, ByVal strFilename) Dim FSO Dim strFullPathToSearch Dim objSubFolders, subfolder Set FSO = CreateObject("Scripting.FileSystemObject") 'Initialize function FindFileRecursively = "" 'Check that filename is not empty If strFileName = "" Then Exit Function 'Get full file pathname strFullPathToSearch = strRootFolder & "\" & strFilename 'Check if root folder exists If FSO.FolderExists(strRootFolder) Then 'Check if file exists under root folder If FSO.FileExists(strFullPathToSearch) Then FindFileRecursively = strFullPathToSearch Else 'Get subfolders Set objSubFolders = FSO.GetFolder(strRootFolder).SubFolders For Each subfolder in objSubFolders strFullPathToSearch = strRootFolder & "\" & subfolder.name FindFileRecursively = FindFileRecursively(strFullPathToSearch, strFilename) If FindFileRecursively <> "" Then Exit For End If Next End If End If End Function
转载于:https://www.cnblogs.com/yongfeiuall/archive/2013/01/10/4134204.html