Public Function Findfile(MyPath As String, Expand As String, MyList As ListBox)
Dim Myname As String
Dim dir_i() As String
Dim i, idir As Long
Dim str_FileNameList() As String
Dim date_CreatTime() As Date
Dim count1 As Integer
Dim int_TotalFileNum As Integer
Dim fso As New FileSystemObject
Dim file_Treat As File
MyList.Clear
If Right(MyPath, 1) <> "\" Then MyPath = MyPath + "\"
Myname = Dir(MyPath, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
int_TotalFileNum = 0
Do While Myname <> ""
If Myname <> "." And Myname <> ".." Then
If (GetAttr(MyPath & Myname) And vbDirectory) = vbDirectory Then '如果找到的是目录
' idir = idir + 1
' ReDim Preserve dir_i(idir) As String
' dir_i(idir - 1) = Myname
Else
If UCase(Right(Myname, 3)) = UCase(Expand) Then
' MyList.AddItem Replace(UCase(Myname), UCase("." & Expand), "") '把找到的文件显示到列表框中
int_TotalFileNum = int_TotalFileNum + 1
ReDim Preserve str_FileNameList(int_TotalFileNum)
str_FileNameList(int_TotalFileNum) = Myname
End If
End If
End If
Myname = Dir '搜索下一项
Loop
If int_TotalFileNum > 0 Then
ReDim Preserve date_CreatTime(int_TotalFileNum)
For count1 = 1 To int_TotalFileNum
Set file_Treat = fso.GetFile(MyPath & str_FileNameList(count1))
date_CreatTime(count1) = file_Treat.DateCreated
Next
'OrderByCreatTime date_CreatTime(), str_FileNameList() '按时间进行排序
OrderByName str_FileNameList()
For count1 = 1 To int_TotalFileNum
MyList.AddItem Replace(UCase(str_FileNameList(count1)), UCase("." & Expand), "")
'Debug.Print date_CreatTime(count1) & " : " & str_FileNameList(count1)
Next
End If
End Function
Private Sub OrderByName(ByRef FileName() As String)
Dim i As Integer
Dim J As Integer
Dim length As Integer
Dim temp
length = UBound(FileName)
For i = 1 To length - 1
For J = 2 To length - i + 1
If Val(FileName(J - 1)) > Val(FileName(J)) Then
temp = FileName(J - 1)
FileName(J - 1) = FileName(J)
FileName(J) = temp
End If
Next
Next
End Sub
Private Sub OrderByCreatTime(ByRef CreatTime() As Date, ByRef FileName() As String)
Dim i As Integer
Dim J As Integer
Dim length As Integer
Dim temp
length = UBound(CreatTime)
For i = 1 To length - 1
For J = 2 To length - i + 1
If CreatTime(J - 1) < CreatTime(J) Then
temp = CreatTime(J - 1)
CreatTime(J - 1) = CreatTime(J)
CreatTime(J) = temp
temp = FileName(J - 1)
FileName(J - 1) = FileName(J)
FileName(J) = temp
End If
Next
Next
End Sub