下面是我自己写的一个函数,专门用来查找文件的,自己经常在用,速度还算可以,你看看吧,API的定义部份我需要整理一下,太多,搞不清用了哪些了Public Function FindFilesAPI(ByVal Path As String, ByVal SearchStr As String, ByRef filecount As Long, ByRef DirCount As Long, ByVal ControlName As ListView, Optional ByVal ShowPath As Boolean = False) As Long Dim FileName As String ' 文件名 Dim DirName As String ' 子目录名 Dim dirNames() As String ' 目录数组 Dim nDir As Integer ' 当前路径的目录数 Dim i As Integer ' 循环计数器变量 Dim hSearch As Long ' 搜索句柄变量 Dim WFD As WIN32_FIND_DATA Dim Cont As Integer If Right(Path, 1) <> "\" Then Path = Path & "\" '搜索子目录 nDir = 0 ReDim dirNames(nDir) Cont = True hSearch = FindFirstFile(Path & "*", WFD) If hSearch <> INVALID_HANDLE_VALUE Then Do While Cont DirName = StripNulls(WFD.cFileName) If (DirName <> ".") And (DirName <> "..") Then If GetFileAttributes(Path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then dirNames(nDir) = DirName DirCount = DirCount + 1 nDir = nDir + 1 ReDim Preserve dirNames(nDir) End If End If Cont = FindNextFile(hSearch, WFD) '获取下一个子目录 Loop Cont = FindClose(hSearch) End If ' 遍历目录并累计文件总数 hSearch = FindFirstFile(Path & SearchStr, WFD) Cont = True If hSearch <> INVALID_HANDLE_VALUE Then While Cont FileName = StripNulls(WFD.cFileName) If (FileName <> ".") And (FileName <> "..") Then FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow filecount = filecount + 1 If ShowPath Then ControlName.ListItems.Add , Path & FileName, Path & FileName Else ControlName.ListItems.Add , Path & FileName, FileName End If End If Cont = FindNextFile(hSearch, WFD) ' 获取下一个文件 Wend Cont = FindClose(hSearch) End If '如果子目录存在则遍历 If nDir > 0 Then For i = 0 To nDir - 1 FindFilesAPI = FindFilesAPI + FindFilesAPI(Path & dirNames(i) & "\", SearchStr, filecount, DirCount, ControlName) Next i End If End Function函数需要传入一个ListView控件作为参数,搜到的文件都放在ListView中
定义部分,不知道有漏掉的没,你试试看吧,如果有自己加上 Public Const MAXDWORD = &HFFFF Public Const INVALID_HANDLE_VALUE = -1 Public Const FILE_ATTRIBUTE_ARCHIVE = &H20 Public Const FILE_ATTRIBUTE_DIRECTORY = &H10 Public Const FILE_ATTRIBUTE_HIDDEN = &H2 Public Const FILE_ATTRIBUTE_NORMAL = &H80 Public Const FILE_ATTRIBUTE_READONLY = &H1 Public Const FILE_ATTRIBUTE_SYSTEM = &H4 Public Const FILE_ATTRIBUTE_TEMPORARY = &H100 Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
'引用MS Scripting Runtime Dim Filesys As New FileSystemObject Dim Folder As Folder Dim File As File Set Folder = Filesys.GetFolder("D:\asset") For Each File In Folder.Files If File.Name = "1.txt" Then MsgBox "1.txt ÒÑÕÒµ½£¡" End If Next
Private Sub Command1_Click() Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists("D:\soft\1.txt") = False Then MsgBox "no" If objFSO.FileExists("D:\soft\1.txt") = True Then MsgBox "yes" End Sub
Private Sub Command1_Click() If Dir("c:\bmp\a4.bmp") <> "" Then MsgBox "有" Else MsgBox "没有" End If End Sub
Dim FileName As String ' 文件名
Dim DirName As String ' 子目录名
Dim dirNames() As String ' 目录数组
Dim nDir As Integer ' 当前路径的目录数
Dim i As Integer ' 循环计数器变量
Dim hSearch As Long ' 搜索句柄变量
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer
If Right(Path, 1) <> "\" Then Path = Path & "\"
'搜索子目录
nDir = 0
ReDim dirNames(nDir)
Cont = True
hSearch = FindFirstFile(Path & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While Cont
DirName = StripNulls(WFD.cFileName)
If (DirName <> ".") And (DirName <> "..") Then
If GetFileAttributes(Path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
dirNames(nDir) = DirName
DirCount = DirCount + 1
nDir = nDir + 1
ReDim Preserve dirNames(nDir)
End If
End If
Cont = FindNextFile(hSearch, WFD) '获取下一个子目录
Loop
Cont = FindClose(hSearch)
End If
' 遍历目录并累计文件总数
hSearch = FindFirstFile(Path & SearchStr, WFD)
Cont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While Cont
FileName = StripNulls(WFD.cFileName)
If (FileName <> ".") And (FileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
filecount = filecount + 1
If ShowPath Then
ControlName.ListItems.Add , Path & FileName, Path & FileName
Else
ControlName.ListItems.Add , Path & FileName, FileName
End If
End If
Cont = FindNextFile(hSearch, WFD) ' 获取下一个文件
Wend
Cont = FindClose(hSearch)
End If
'如果子目录存在则遍历
If nDir > 0 Then
For i = 0 To nDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(Path & dirNames(i) & "\", SearchStr, filecount, DirCount, ControlName)
Next i
End If
End Function函数需要传入一个ListView控件作为参数,搜到的文件都放在ListView中
Public Const MAXDWORD = &HFFFF
Public Const INVALID_HANDLE_VALUE = -1
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Dim Filesys As New FileSystemObject
Dim Folder As Folder
Dim File As File
Set Folder = Filesys.GetFolder("D:\asset")
For Each File In Folder.Files
If File.Name = "1.txt" Then
MsgBox "1.txt ÒÑÕÒµ½£¡"
End If
Next
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("D:\soft\1.txt") = False Then MsgBox "no"
If objFSO.FileExists("D:\soft\1.txt") = True Then MsgBox "yes"
End Sub
If Dir("c:\bmp\a4.bmp") <> "" Then
MsgBox "有"
Else
MsgBox "没有"
End If
End Sub