|
Eutopian 发表于 2013-4-25 20:56
當時正在學習到FOS對象, 在論壇上看到這個帖子, 一時也想試試, 於是也寫了一段代碼. 最初也嘗試過用遞歸的 ...
下面是搜寻并列出所有符合条件的文件的递归代码:- Public flist$(65535, 3), fc&, fs&, k&, s$
- Sub FileList()
-
- s = InputBox("Please input File's Ext type:", "Find Files", "xl")
- If s = "" Then Exit Sub Else s = LCase(s) & "*"
- pth = InputBox("Confirm FileFolder Path:", "Find Files", ThisWorkbook.Path)
-
- k = 0: fc = 0: fs = 0: tms = Timer
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set fld = fso.GetFolder(pth)
- t = 0
- For Each f In fld.Files
- n = InStrRev(f.Name, ".")
- If n Then
- x = LCase(Mid(f.Name, n + 1))
- If x Like s Then
- t = 1
- flist(k, 0) = x
- flist(k, 1) = f.Name
- flist(k, 2) = fld.Name
- flist(k, 3) = fld.Path
- k = k + 1
- End If
- End If
- Next
- If t Then fs = fs + 1
- fc = fc + 1
- Call GetFolderFile(pth)
-
- [a1].CurrentRegion.Offset(1) = ""
- If k Then [a2].Resize(k, 4) = flist
- [b1] = "Check " & fc & " SubFolders Get " & k & " Files from " & fs & " Folders."
- MsgBox Format(Timer - tms, "0.000s")
-
- End Sub
- Function GetFolderFile(pth)
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set fld = fso.GetFolder(pth)
- Set fsb = fld.SubFolders
- For Each fd In fsb
- t = 0
- For Each f In fd.Files
- n = InStrRev(f.Name, ".")
- If n Then
- x = LCase(Mid(f.Name, n + 1))
- If x Like s Then
- t = 1
- flist(k, 0) = x
- flist(k, 1) = f.Name
- flist(k, 2) = fd.Name
- flist(k, 3) = fd.Path
- k = k + 1
- End If
- End If
- Next
- If t Then fs = fs + 1
- fc = fc + 1: Call GetFolderFile(fd.Path)
- Next
- End Function
复制代码 递归的做法其实是一样的。 |
评分
-
1
查看全部评分
-
|