一、建立工程
首先,我们需要建立一个新的VBA项目,打开Visual Basic Editor,按下ALT+F11,在弹出的窗口中选择“插入”-“模块”,即可新建一个空的模块。
Sub TraverseFolders()
End Sub
二、定义变量
在Sub过程中,我们可以定义多个变量,变量名可以自由命名,但类型需要定义为“String”或“Variant”,这里定义了一个主文件夹路径MainFolder
,以及ListObject对象tbl
用于存储所有文件路径。
Sub TraverseFolders()
Dim MainFolder As String
Dim tbl As ListObject
End Sub
三、定义ListObject表格
ListObject表格是一种强大的表格功能,拥有各种排序、筛选、格式控制功能等等。这里我们定义一个新的表格,命名为“FileList”,将表头设置为“File Name”和“Path”,并将其填充至工作表的A1单元格。
Sub TraverseFolders()
Dim MainFolder As String
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B1"), , xlYes)
tbl.Name = "FileList"
tbl.HeaderRowRange.Cells(1, 1).Value = "File Name"
tbl.HeaderRowRange.Cells(1, 2).Value = "Path"
End Sub
四、定义递归过程
递归是一种经典的程序设计思路,用于处理一些可分解为多个子问题的问题,比如树形数据结构、文件夹中的文件等等。这里我们定义一个子过程TraverseSubfolders,它接受两个参数,FolderPath
表示当前文件夹路径,tbl
表示传入的ListObject表格。
Sub TraverseFolders()
Dim MainFolder As String
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B1"), , xlYes)
tbl.Name = "FileList"
tbl.HeaderRowRange.Cells(1, 1).Value = "File Name"
tbl.HeaderRowRange.Cells(1, 2).Value = "Path"
TraverseSubfolders MainFolder, tbl
End Sub
Sub TraverseSubfolders(FolderPath As String, tbl As ListObject)
End Sub
五、遍历文件夹并加载子文件夹
在TraverseSubfolders过程中,我们需要使用VBA内置函数FileSystemObject(FSO)来处理文件夹和文件。首先,我们需要在过程开始时判断指定路径是否为文件夹,如果是,则遍历文件夹内所有文件并添加到ListObject表格中,否则递归处理子文件夹。注意,在递归过程中需要先处理所有的子文件夹才能处理当前文件夹。
Sub TraverseFolders()
Dim MainFolder As String
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B1"), , xlYes)
tbl.Name = "FileList"
tbl.HeaderRowRange.Cells(1, 1).Value = "File Name"
tbl.HeaderRowRange.Cells(1, 2).Value = "Path"
TraverseSubfolders MainFolder, tbl
End Sub
Sub TraverseSubfolders(FolderPath As String, tbl As ListObject)
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim folder As Folder
Set folder = fso.GetFolder(FolderPath)
Dim file As File
For Each file In folder.Files
Dim newRow As ListRow
Set newRow = tbl.ListRows.Add
newRow.Range.Cells(1, 1).Value = file.Name
newRow.Range.Cells(1, 2).Value = file.Path
Next
Dim subFolder As Folder
For Each subFolder In folder.SubFolders
TraverseSubfolders subFolder.Path, tbl
Next
End Sub
六、完整代码
Sub TraverseFolders()
Dim MainFolder As String
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1:B1"), , xlYes)
tbl.Name = "FileList"
tbl.HeaderRowRange.Cells(1, 1).Value = "File Name"
tbl.HeaderRowRange.Cells(1, 2).Value = "Path"
TraverseSubfolders MainFolder, tbl
End Sub
Sub TraverseSubfolders(FolderPath As String, tbl As ListObject)
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim folder As Folder
Set folder = fso.GetFolder(FolderPath)
Dim file As File
For Each file In folder.Files
Dim newRow As ListRow
Set newRow = tbl.ListRows.Add
newRow.Range.Cells(1, 1).Value = file.Name
newRow.Range.Cells(1, 2).Value = file.Path
Next
Dim subFolder As Folder
For Each subFolder In folder.SubFolders
TraverseSubfolders subFolder.Path, tbl
Next
End Sub
七、总结
我们通过使用VBA内置的FileSystemObject对象,在遍历文件夹中的所有文件时实现了递归遍历所有子文件夹的功能,将所有文件名和路径存储在ListObject表格中。在实际应用中,我们可以进一步扩展这个功能,比如筛选指定类型的文件、添加文件属性和标签、自动处理重复文件等等。