我试图创建一个递归函数,将每个文件夹(和子文件夹)添加到自定义对象的集合中。我的代码适用于大约75%的文件夹/子文件夹,但从集合中错过了看似随机的文件。
有什么想法吗?
FolderObj是一个自定义类,我添加的集合称为ToPathList
Option Explicit
Sub RecurseFolderList(Foldername As String)
On Error Resume Next
Dim FSO, NextFolder
Dim OriginalRange As Range
Dim tempFolderObj As FolderObj
Dim i As Integer
i = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
If Err.Number > 0 Then
Exit Sub
End If
If FSO.FolderExists(Foldername) Then
Set NextFolder = FSO.GetFolder(Foldername)
Set FolderArray = NextFolder.subfolders
For Each NextFolder In FolderArray
Set tempFolderObj = New FolderObj
'assign variables to temporary object
With tempFolderObj
.ID = i
.Filename = NextFolder.Name
.path = NextFolder.path
.first3ints = first3Non0Ints(NextFolder.Name)
End With
'add temporary object to colelction
Call ToPathList.Add(tempFolderObj, CStr(i))
i = i + 1
RecurseFolderList (NextFolder)
Next
' Set NextFolder = Nothing
' Set FolderArray = Nothing
End If
Set FSO = Nothing
End Sub
答案 0 :(得分:0)
我的键(i)在函数内声明,因此每次递归函数时都会重置为'1'。
这导致了非唯一键,因此某些项目未添加到集合中。
感谢大家的帮助。