首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VBA怎么遍历某个目录下的所有子目录

2012-01-19 
VBA如何遍历某个目录下的所有子目录aaaaaa\bbbaaa\bbb\cccaaa\bbb\ccc\dddaaa\bbb\ccc\ddd\eeeaaa\bbb\ccc

VBA如何遍历某个目录下的所有子目录
aaa
aaa\bbb
aaa\bbb\ccc
aaa\bbb\ccc\ddd
aaa\bbb\ccc\ddd\eee
aaa\bbb\ccc\ddd\eee\fff
比如上面的目录结果,如果指定aaa,如何遍历aaa下面所有的子目录。一直到fff这个级别。
将子目录做成列表或者集合。

[解决办法]

VB code
Dim strrec$(), TFolders&Dim i&, fs, f, sPrivate Sub Command1_Click()   s = GetFolders("e:\music")   If TFolders <= 0 Then MsgBox "无子文件夹": Exit Sub   Me.Cls   For i = 0 To UBound(s)      Print s(i)   Next i   MsgBox "总共有: " & CStr(TFolders) & " 个子文件夹"End SubPublic Function GetFolders(Selpath$) As String()   Set fs = CreateObject("Scripting.FileSystemObject")   Selpath = IIf(Right(Selpath, 1) = "\", Mid(Selpath, 1, Len(Selpath) - 1), Selpath)   Set f = fs.GetFolder(Selpath)   ReDim Preserve strrec$(0)   strrec(0) = Selpath   TFolders = 0   GetSubFolder f, 0   GetFolders = strrec()End FunctionPrivate Sub GetSubFolder(f, n)   Dim sf, f1   Set sf = f.SubFolders   If Not sf Is Nothing Then      For Each f1 In sf         GetSubFolder f1, n + 4         TFolders = TFolders + 1         ReDim Preserve strrec$(TFolders - 1)         strrec(TFolders - 1) = f1         DoEvents      Next   End IfEnd Sub本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cbm666/archive/2008/03/08/2158251.aspx 

热点排行