Option Explicit
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 255
cAlternate As String * 14
End Type
Private Sub cmdFindFiles_Click()
Dim wfd As WIN32_FIND_DATA
Dim isFindHandle As Long
Dim FileCount As Long
Dim Out As Long
Dim fileName As String
Out = True
isFindHandle = FindFirstFile("D:\vb\*", wfd)
If isFindHandle <> -1 Then
Do While Out
fileName = StripNulls(wfd.cFileName)
If fileName <> "." And fileName <> ".." Then
FileCount = FileCount + 1
List1.AddItem fileName
End If
Out = FindNextFile(isFindHandle, wfd)
Loop
End If
FindClose isFindHandle
MsgBox "D:\vb\中共有文件与文件夹数:" & FileCount
End Sub
Private Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function