参考一下该例子:
Public Function TreeSearch(ByVal sPath As String, ByVal sFileSpec As String, sFiles() As String) As Long
Static Files As Long '文件数目
Dim sDir As String
Dim sSubDirs() As String '存放子目录名称
Dim Index As Long
If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
sDir = Dir(sPath & sFileSpec)
'获得当前目录下文件名和数目
Do While Len(sDir)
Files = Files + 1
ReDim Preserve sFiles(1 To Files)
sFiles(Files) = sPath & sDir
sDir = Dir
Loop
'获得当前目录下的子目录名称
Index = 0
sDir = Dir(sPath & "*.*", 16)
Do While Len(sDir)
If Left(sDir, 1) <> "." Then 'skip.and..
'找出子目录名
If GetAttr(sPath & sDir) And vbDirectory Then
Index = Index + 1
'保存子目录名
ReDim Preserve sSubDirs(1 To Index)
sSubDirs(Index) = sPath & sDir & "\"
End If
End If
sDir = Dir
Loop
For Index = 1 To Index
'查找每一个子目录下文件,这里利用了递归
Call TreeSearch(sSubDirs(Index), sFileSpec, sFiles())
Next Index
TreeSearch = Files
End Function
Option Explicit
Private objFileSystem As FileSystemObject
Private Sub DisplayPath(ByVal strPath As String)
Dim objFolder As Folder
Dim objSubFolder As Folder
Dim objFile As File
Dim objItem As ListItem
' Check for the existence of the path
If Not objFileSystem.FolderExists(strPath) Then
Err.Raise vbObjectError+6003, "DisplayPath", "Path " & strPath & " does not exist."
Exit Sub
End If
' Set a reference to the folder
Set objFolder = objFileSystem.GetFolder(strPath)
' Traverse the folders and load them in the ListView control
For Each objSubFolder In objFolder.SubFolders
Set objItem = lvFileObjects.ListItems.Add(, , _
objSubFolder.Name, "closed", "closed")
objItem.SubItems(1) = Format(objSubFolder.Size, "#,###")
objItem.SubItems(2) = objSubFolder.Type
objItem.SubItems(3) = objSubFolder.ShortName
' Folders without a created date are set to 1/1/80
If objSubFolder.DateCreated <> "1/1/80" Then
objItem.SubItems(4) = objSubFolder.DateCreated
End If
objItem.SubItems(5) = objSubFolder.DateLastModified
objItem.SubItems(6) = objSubFolder.DateLastAccessed
Next
' Traverse the files and load them in the ListView control
For Each objFile In objFolder.Files
Set objItem = lvFileObjects.ListItems.Add(, , _
objFile.Name, "leaf", "leaf")
objItem.SubItems(1) = Format(objFile.Size, "#,###")
objItem.SubItems(2) = objFile.Type
objItem.SubItems(3) = objFile.ShortName
' Files without a created date are set to 1/1/80
If objFile.DateCreated <> "1/1/80" Then
objItem.SubItems(4) = objFile.DateCreated
End If
objItem.SubItems(5) = objFile.DateLastModified
objItem.SubItems(6) = objFile.DateLastAccessed
Next