|
Private Sub CommandButton1_Click()
Dim fs
Dim mypath As String
Dim theSh As Object
Dim theFolder As Object
On Error Resume Next
Range("A2:B65536").ClearContents '表中的数据清除
'设置搜索目录
Set theSh = CreateObject("shell.application")
Set theFolder = theSh.BrowseForFolder(0, "", 0, "")
If Not theFolder Is Nothing Then
mypath = theFolder.Items.Item.Path
End If
'//////////////搜索开始//////////////
Set fs = Application.FileSearch
With fs
.NewSearch
.SearchSubFolders = True ''搜索子目录
.LookIn = mypath '搜索路径
.Filename = "*.*" '搜索文件的种类
If .Execute(SortBy:=msoSortByFileName) > 0 Then
c = .FoundFiles.Count '统计搜索到的文件个数
MsgBox "在这里找到" & .FoundFiles.Count & _
"文件" '输出搜索到的文件个数
For i = 1 To c
strTemp = .FoundFiles(i) '设置临时文件
n = InStrRev(strTemp, "\") '获取文件路径长度(不包括文件名)
strfilename = Replace(strTemp, Left(strTemp, n), "") '获取文件名及扩展名
Cells(i + 1, 1) = Left(strfilename, Len(strfilename) - 0) '输出格式:文件名+扩展名
Cells(i + 1, 2) = Left(strTemp, Len(strTemp) - Len(strfilename)) '输出格式:文件路径
Next
End If
End With
Set fs = Nothing
End Sub
希望能帮到你 |
|