|
本帖最后由 ludaoqi 于 2020-4-29 22:45 编辑
这个才是递归吧,递归就是调用其本身.
这个思路是先判断有没有文件,有文件先保存出来,然后在IF判断有没有文件夹,有子文件夹就调用本身,在重复这操作.知道所有的文件夹都遍历完.
其实隐藏这两个IF也可以,因为条件存在是默认的.如果不满足条件for循坏就会跳过.
Sub SelectFile()
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then Path = .SelectedItems(1) Else Exit Sub
End With
ActiveSheet.Cells.Clear
Call GetFolderFiles(Path)
End Sub
Function GetFolderFiles(Path)
Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.GetFolder(Path).Files.Count > 0 Then
For Each FileObj In FSO.GetFolder(Path).Files
ActiveSheet.Cells(65536, 1).End(xlUp).Offset(1, 0) = FileObj
Next
End If
If FSO.GetFolder(Path).SubFolders.Count Then
For Each FolderObj In FSO.GetFolder(Path).SubFolders
Call GetFolderFiles(FolderObj)
Next
End If
End Function
|
|