'一个搜索文件的源代码,供参考
Private findfile As Long
Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
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 * MAX_PATH
cAlternate As String * 14
End Type
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
'刲坰恅璃
' Spath 猁刲坰腔繚噤 瞰: "C:\windows"
' SFile 猁刲坰腔恅璃靡 瞰: "*.exe"
' FindSubDirectory=true 刲坰赽醴翹
Public Function AllSearch(ByVal Spath As String, ByVal sFile As String, ByVal FindSubDirectory As Boolean) As Boolean
Dim xf As WIN32_FIND_DATA
Dim ff As WIN32_FIND_DATA
Dim findhandle As Long
Dim lFindFile As Long
Dim astr As String
Dim bstr As String
DoEvents
If Len(Trim(Spath)) = 3 Then
Else
Spath = Trim(Spath) + "\"
End If
lFindFile = FindFirstFile(Spath + sFile, ff)
If lFindFile > 0 Then
Do
'Find
DoEvents
Form1.List1.AddItem Spath + Trim(ff.cFileName)
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If
If FindSubDirectory = False Then
If Form1.List1.ListCount >= 1 Then
AllSearch = True
Else
AllSearch = False
End If
Exit Function
End If
astr = Spath + "*.*"
findhandle = FindFirstFile(astr, xf)
DoEvents
Do
If xf.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
If Asc(xf.cFileName) <> Asc(".") Then
bstr = Spath + Left$(xf.cFileName, InStr(xf.cFileName, Chr(0)) - 1)
DoEvents
AllSearch bstr, sFile, True
End If
End If
xf.cFileName = ""
DoEvents
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose findfile
If Form1.List1.ListCount >= 1 Then
AllSearch = True
Else
AllSearch = False
End If
End Function
Private findfile As Long
Private Const MAX_PATH = 260
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
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 * MAX_PATH
cAlternate As String * 14
End Type
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
'刲坰恅璃
' Spath 猁刲坰腔繚噤 瞰: "C:\windows"
' SFile 猁刲坰腔恅璃靡 瞰: "*.exe"
' FindSubDirectory=true 刲坰赽醴翹
Public Function AllSearch(ByVal Spath As String, ByVal sFile As String, ByVal FindSubDirectory As Boolean) As Boolean
Dim xf As WIN32_FIND_DATA
Dim ff As WIN32_FIND_DATA
Dim findhandle As Long
Dim lFindFile As Long
Dim astr As String
Dim bstr As String
DoEvents
If Len(Trim(Spath)) = 3 Then
Else
Spath = Trim(Spath) + "\"
End If
lFindFile = FindFirstFile(Spath + sFile, ff)
If lFindFile > 0 Then
Do
'Find
DoEvents
Form1.List1.AddItem Spath + Trim(ff.cFileName)
Loop Until (FindNextFile(lFindFile, ff) = 0)
FindClose lFindFile
End If
If FindSubDirectory = False Then
If Form1.List1.ListCount >= 1 Then
AllSearch = True
Else
AllSearch = False
End If
Exit Function
End If
astr = Spath + "*.*"
findhandle = FindFirstFile(astr, xf)
DoEvents
Do
If xf.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY Then
If Asc(xf.cFileName) <> Asc(".") Then
bstr = Spath + Left$(xf.cFileName, InStr(xf.cFileName, Chr(0)) - 1)
DoEvents
AllSearch bstr, sFile, True
End If
End If
xf.cFileName = ""
DoEvents
Loop Until (FindNextFile(findhandle, xf) = 0)
FindClose findfile
If Form1.List1.ListCount >= 1 Then
AllSearch = True
Else
AllSearch = False
End If
End Function
Dim nI As Integer, nDirectory As Integer, i As Long
Dim sFileName As String, sDirectoryList() As String
'First list all normal files in this directory
sFileName = Dir(CurrentPath, vbHidden Or vbDirectory Or vbReadOnly Or vbSystem)
Do While sFileName <> ""
If UCase(sFileName) Like UCase(SearFile) Then
i = GetAttr(CurrentPath + sFileName)
If (i And vbDirectory) = 0 Then
ReDim Preserve FoundFile(ntx)
FoundFile(ntx) = CurrentPath + sFileName
ntx = ntx + 1
End If
End If
If sFileName <> "." And sFileName <> ".." Then
'Ignore nondirectories
If GetAttr(CurrentPath & sFileName) _
And vbDirectory ThennDirectory = nDirectory + 1
ReDim Preserve sDirectoryList(nDirectory)
sDirectoryList(nDirectory) = CurrentPath & sFileName
End If
End If
sFileName = Dir
Loop
'Recursively process each directory
For nI = 1 To nDirectory
searchDir sDirectoryList(nI) & "\", SearFile
Next nI
End Sub