|

楼主 |
发表于 2010-4-12 00:08
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
部分源碼
Option Explicit
Option Base 1
Option Compare Text
Private 檔案系統 As Object
Private 檔案資訊陣列() As String
Sub 搜尋檔案(資料夾路徑 As String, 篩選檔名 As String, 包含子資料夾 As Boolean)
On Error Resume Next
Dim X As Long
Dim Y As Long
Dim N As Long
Dim K As Long
Dim F As Long
Dim 檔名 As String
Dim 資料夾路徑陣列() As String
Set 檔案系統 = CreateObject("Scripting.FileSystemObject")
ReDim 資料夾路徑陣列(256, 1) As String
資料夾路徑陣列(1, 1) = 資料夾路徑
N = 0
F = 1
For X = 1 To 256
If Not 資料夾路徑陣列(X, 1) = "" Then
K = 0
For Y = 1 To F
If Not 資料夾路徑陣列(X, Y) = "" Then
檔名 = Dir(資料夾路徑陣列(X, Y) & "*", 31)
Do Until 檔名 = ""
If Not 檔名 = "." And Not 檔名 = ".." Then
If (GetAttr(資料夾路徑陣列(X, Y) & 檔名) And vbDirectory) = vbDirectory Then
If 包含子資料夾 = True Then
K = K + 1
If K > F Then
F = F + 1
ReDim Preserve 資料夾路徑陣列(256, F) As String
End If
資料夾路徑陣列(X + 1, K) = 資料夾路徑陣列(X, Y) & 檔名 & "\"
End If
Else
If 檔名 Like 篩選檔名 Then
N = N + 1
Application.StatusBar = "搜尋檔案中......第 " & N & " 筆 " & 檔名
ReDim Preserve 檔案資訊陣列(7, N) As String
檔案資訊陣列(1, N) = 資料夾路徑陣列(X, Y) & 檔名
檔案資訊陣列(2, N) = FileLen(資料夾路徑陣列(X, Y) & 檔名)
檔案資訊陣列(3, N) = "'" & 檔案系統.GetExtensionName(資料夾路徑陣列(X, Y) & 檔名)
檔案資訊陣列(4, N) = "'" & Application.WorksheetFunction.Text(FileDateTime(資料夾路徑陣列(X, Y) & 檔名), "yyyy/mm/dd hh:mm:ss")
If (GetAttr(資料夾路徑陣列(X, Y) & 檔名) And vbReadOnly) = vbReadOnly Then
檔案資訊陣列(5, N) = "R"
End If
If (GetAttr(資料夾路徑陣列(X, Y) & 檔名) And vbHidden) = vbHidden Then
檔案資訊陣列(5, N) = 檔案資訊陣列(5, N) & "H"
End If
If (GetAttr(資料夾路徑陣列(X, Y) & 檔名) And vbSystem) = vbSystem Then
檔案資訊陣列(5, N) = 檔案資訊陣列(5, N) & "S"
End If
If (GetAttr(資料夾路徑陣列(X, Y) & 檔名) And vbArchive) = vbArchive Then
檔案資訊陣列(5, N) = 檔案資訊陣列(5, N) & "A"
End If
檔案資訊陣列(6, N) = 資料夾路徑陣列(X, Y)
檔案資訊陣列(7, N) = "'" & 檔名
End If
End If
End If
檔名 = Dir
Loop
Else
Exit For
End If
Next Y
Else
Exit For
End If
Next X
Application.StatusBar = "完成搜尋,共 " & N & " 筆"
End Sub |
|