vba 某路径下有多少个子文件夹

如题所述

Private MyDirectory() As String
Private n As Long

Sub MyMain()
Dim i As Integer
n = 0
'调用自定义递归过程
Call FindMyPath("C:\AAA\BBB\") '假设要搜索"C:\AAA\BBB\”里的所有子文件夹
'输出所有子文件夹
For i = 1 To UBound(MyDirectory)
Debug.Print MyDirectory(i)
Next i
'输出子文件夹总数
Debug.Print UBound(MyDirectory)
End Sub

'自定义递归过程
Private Sub FindMyPath(ByVal MyPath As String)
Dim MyDirCount As Long
Dim MyDirs() As String
Dim MyResult As String
Dim i As Long

MyDirCount = 0
If Right$(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
MyResult = Dir(MyPath, vbDirectory + vbSystem + vbReadOnly + vbHidden + vbNormal + vbArchive)
Do While Len(MyResult) > 0
DoEvents
If MyResult <> "." And MyResult <> ".." Then
If (GetAttr(MyPath & MyResult) And vbDirectory) = vbDirectory Then
n = n + 1
ReDim Preserve MyDirectory(n)
MyDirectory(n) = MyPath & MyResult
MyDirCount = MyDirCount + 1
ReDim Preserve MyDirs(MyDirCount)
MyDirs(MyDirCount) = MyPath & MyResult

End If
End If
MyResult = Dir(, vbDirectory + vbSystem + vbReadOnly + vbHidden + vbNormal + vbArchive)
Loop

For i = 1 To MyDirCount
Call FindMyPath(MyDirs(i))
Next i
End Sub
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网