醉里乾坤大,壶中日月长

从根目录中找指定文件并返回绝对路径

上一篇 / 下一篇  2009-09-08 15:59:53

'———————————————————————–
 Public Function FindFileRecursively(ByVal strRootFolder, ByVal strFilename)
 '———————————————————————–
 '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:
 '    Meir Bar-Tal, SOLMAR Knowledge Networks Ltd.
 '
 'Date:
 '   19-Mar-2009
 '
 '———————————————————————–    
     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 bjSubFolders = 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
'———————————————————————–

MsgBox  FindFileRecursively("c:\","IntelGFX.log")

'来自:http://www.advancedqtp.com/knowledge-base/qtips/file-system-id6/files-id20/filesystemobject-id22/find-a-file-recursively-revised/

TAG:

 

评分:0

我来说两句

Open Toolbar