|
楼主 |
发表于 2015-2-4 12:16
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
其实不用字典也可以。但是要使用Redim数组,并不断地更新数组大小……这让代码看上去有点烦。
解释暂略(因为很上面一样的)- Sub ListAllDirTest()
- With Application.FileDialog(msoFileDialogFolderPicker)
- If .Show Then myPath$ = .SelectedItems(1) Else Exit Sub
- End With
- If Right(myPath, 1) <> "" Then myPath = myPath & ""
-
- MsgBox Join(ListAllDir(myPath), vbCr) 'GetAllSubFolder's File
- MsgBox Join(ListAllDir(myPath, 1), vbCr) 'GetThisFolder's File
-
- MsgBox Join(ListAllDir(myPath, -1), vbCr) 'GetThisFolder's SubFolder
- MsgBox Join(ListAllDir(myPath, -2), vbCr) 'GetAllSubFolder
-
- MsgBox Join(ListAllDir(myPath, 1, "tst"), vbCr) 'GetThisFolder's SpecialFile
- MsgBox Join(ListAllDir(myPath, , "tst"), vbCr) 'GetAllSubFolder's SpecialFile
-
- End Sub
- Function ListAllDir(myPath$, Optional sb& = 0, Optional SpFile$ = "")
- Dim i&, j&, k&, myFile$
- ReDim fld(0), file(0) '定义可变数组fld存放子文件夹路径、file存放文件名
-
- fld(0) = myPath '子文件夹初始化写入指定目标文件夹路径
- On Error Resume Next
- Do
- myFile = Dir(fld(i), vbDirectory)
- Do While myFile <> ""
- If myFile <> "." And myFile <> ".." Then
- If (GetAttr(fld(i) & myFile) And vbDirectory) = vbDirectory Then
- If Err.Number Then Err.Clear Else j = j + 1: ReDim Preserve fld(j): fld(j) = fld(i) & myFile & ""
- Else
- If SpFile = "" Then
- file(k) = myFile: k = k + 1: ReDim Preserve file(k)
- Else
- If InStr(myFile, SpFile) Then file(k) = myFile: k = k + 1: ReDim Preserve file(k)
- End If
- End If
- End If
- myFile = Dir
- Loop
- If sb Mod 2 Then Exit Do Else i = i + 1
- Loop Until i > UBound(fld)
- If sb >= 0 Or Len(SpFile) Then ListAllDir = file Else ListAllDir = fld
- End Function
复制代码 |
|