|
楼主 |
发表于 2016-1-17 10:37
|
显示全部楼层
本帖最后由 小花鹿 于 2016-1-17 10:39 编辑
搜索选定文件夹及其子文件夹中文件的小工具:(用窗体实时显示搜索到的文件)
Option Explicit
Dim FileList(1 To 65536, 1 To 1), filen&
Sub FolderFileList()
'--------------------------------------------------------------------------------------------
Dim myfolder, mypath
Set myfolder = CreateObject("Shell.Application").BrowseForFolder(0, "请选择文件夹:", 0)
If Not myfolder Is Nothing Then
If myfolder <> "桌面" Then mypath = myfolder.Items.Item.Path
Else
MsgBox "你没有选择文件夹!"
Exit Sub
End If
If mypath = "" Or Left(mypath, 2) = "::" Then
MsgBox "文件夹选择错误!"
Exit Sub
End If
'------------------------------------------------------------------------------------------------
Dim fso, Folder, myf
Set fso = CreateObject("scripting.filesystemobject")
Set Folder = fso.getfolder(mypath)
filen = 0
UserForm1.Show 0
For Each myf In Folder.Files
filen = filen + 1
FileList(filen, 1) = myf
UserForm1.Label1.Caption = fileedit(myf)
DoEvents
Next myf
SubFolderFileList (mypath)
UserForm1.Caption = "文件搜索完成:"
UserForm1.Label1.Caption = "文件搜索完成,共搜索到 " & filen & " 个文件!"
DoEvents
If filen > 65536 Then filen = 65536
If filen Then [a1].Resize(filen) = FileList
filen = 0
End Sub
Function SubFolderFileList(pth)
Dim fso, Folder, SubFolder, myf, fd
Set fso = CreateObject("scripting.filesystemobject")
Set Folder = fso.getfolder(pth)
Set SubFolder = Folder.subfolders
On Error Resume Next
For Each fd In SubFolder
For Each myf In fd.Files
If myf <> "" Then
filen = filen + 1
FileList(filen, 1) = myf
UserForm1.Label1.Caption = fileedit(myf)
DoEvents
End If
Next myf
SubFolderFileList (fd.Path)
Next fd
On Error GoTo 0
End Function
Function fileedit(filenam)
Dim file1, file2, filear
If Len(filenam) > 60 Then
filear = Split(filenam, "\")
file2 = filear(UBound(filear))
file1 = Replace(filenam, "\" & file2, "")
file1 = Left(file1, 30)
fileedit = file1 & "...\" & file2
Else
fileedit = filenam
End If
End Function
列出选中文件夹及其子文件夹中的文件.rar
(1.39 MB, 下载次数: 33)
补充内容 (2016-11-4 19:42):
Sub 遍历子文件夹()
Dim d, thispath, thisname, n&, m&, x&, mydir, dk
Set d = CreateObject("scripting.dictionary")
thispath = ThisWorkbook.Path & "\"
thisname = ThisWorkbook.Name
d(thispath) = ""
Do While n < d.Count
dk = d.keys
mydir = Dir(dk(n), vbDirectory)
Do While mydir <> ""
If mydir <> "." And mydir <> ".." Then
If GetAttr(dk(n) & mydir) = vbDirectory Then
d(dk(n) & mydir & "\") = ""
m = m + 1
Cells(m, 1) = dk(n) & mydir & "\"
Else
x = x + 1
Cells(x, 7) = dk(n) & mydir
End If
End If
mydir = Dir
Loop
n = n + 1
Loop
End Sub
|
|