|
本帖最后由 百度不到去谷歌 于 2014-4-25 09:54 编辑
你这个不行的 我的改一下就可以了
你只需要复制我的代码 然后调用的地方
dim arr
arr=GetFiles(thisworkbook.path,0 , "fo")'arr就是所有文件夹及子文件夹的名称数组- '-----------Sub GetFiles----------Copyright@百度不到去谷歌 QQ80871835 2014/3/10---------------------------
- '功能 :'遍历path目录,返回所有文件名或者文件夹名数组,fullname参数可选是否返回完整路径
- '变量 :path string -文件夹路径
- ' Fullname Boolean -是否返回完整路径,默认为true返回完整
- ' ftype string -返回文件还是文件夹,f为文件,fo为文件夹
- '--------------------------------------------------------------------------------------------------
- Function GetFiles(path$, Optional Fullname As Boolean = True, Optional ftype = "f")
- Dim dic As Object, i&
- Set dic = CreateObject("Scripting.Dictionary")
- Dim Fso As Object, Folder As Object
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set Folder = Fso.GetFolder(path)
- i = 1
- Call aGetFile(Folder, dic, ftype)
- If Fullname Then
- GetFiles = dic.keys '返回文件名
- Else
- GetFiles = dic.items '返回完整路径带文件名
- End If
- Set Folder = Nothing
- Set Fso = Nothing
- MsgBox "ok"
- End Function
- Sub aGetFile(ByVal Folder As Object, dic, Optional ftype = "f")
- Dim SubFolder As Object
- Dim File As Object
- If ftype = "fo" Then
- For Each SubFolder In Folder.SubFolders
- dic.Add SubFolder.path, SubFolder.Name
- Call aGetFile(SubFolder, dic, ftype) '递归调用子文件夹
- Next
- Else '遍历文件
- For Each File In Folder.Files '遍历文件
- dic.Add File.path & "" & File.Name, File.Name
- Next
- For Each SubFolder In Folder.SubFolders
- Call aGetFile(SubFolder, dic, ftype) '递归调用子文件夹
- Next
- End If
- End Sub
复制代码 |
|