|
做了个搜索选定文件夹及其子文件夹中文件的小工具,个人觉得挺好玩的。
功能:
1、可以任意选定文件夹
2、搜索选定文件夹、子文件夹、子子文件夹中的所有文件,以便后期处理
3、用窗体实时显示搜索到的文件名及其路径,避免用户认为死机
请选择文件较多的文件夹,比如C: ,看看效果。
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
For Each myf In Folder.Files
UserForm1.Show 0
filen = filen + 1
FileList(filen, 1) = myf
UserForm1.Label1.Caption = fileedit(myf)
DoEvents
Next myf
SubFolderFileList (mypath)
UserForm1.Label1.Caption = "文件搜索完成,共搜索到 " & filen & " 个文件!"
DoEvents
If filen > 65536 Then filen = 65536
[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 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.35 MB, 下载次数: 369)
|
|