当前位置:于振海网 > 文摘 >

vb搜索文件模块,利用api搜索的高效率代码

发布时间:2011年11月21日更新时间:2023年5月24日作者:未知文章ID:512浏览:

Private lindex As Long
Private Pflag As Boolean
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 Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const MAX_PATH = 260
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 * MAX_PATH
        cAlternate As String * 14
End Type
Public Sub GetFileList(sPath As String, sFile As String)
    Dim fso As New FileSystemObject
    Pflag = False
    Form1.ListView1.ListItems.Clear
    lindex = 1
    Screen.MousePointer = vbHourglass
    Form1.StatusBar1.Panels(1).Text = "请稍侯..."
    FindFile Trim(sPath), Trim(sFile)
    Screen.MousePointer = 0
    Form1.StatusBar1.Panels(2).Text = "共有" & Form1.ListView1.ListItems.Count & "个文件"
    Form1.StatusBar1.Panels(1).Text = "就绪"
End Sub
Private Sub FindFile(sPath As String, sFile As String)
    Dim xf As WIN32_FIND_DATA
    Dim ff As WIN32_FIND_DATA
    Dim findhandle As Long
    Dim lFindFile As Long
    Dim Dstr As String
    Dim fso As New FileSystemObject
    Dim f As File
    Dim cPath As String
    cPath = IIf(Len(sPath) > 3, sPath & "\", sPath)
    lFindFile = FindFirstFile(cPath & sFile, ff)
    Form1.StatusBar1.Panels(2).Text = "正在搜索 " & sPath
    If lFindFile > 0 Then
        Do
            Set f = fso.GetFile(cPath & ff.cFileName)
            Form1.ListView1.ListItems.Add
            Form1.ListView1.ListItems(lindex).SubItems(1) = lindex
            Form1.ListView1.ListItems(lindex).SubItems(2) = f.Name
            Form1.ListView1.ListItems(lindex).SubItems(3) = IIf(f.Size < 1024, Format(f.Size, "#### Byte"), Format(f.Size \ 1024, "###### KB"))
            Form1.ListView1.ListItems(lindex).SubItems(4) = Left(f.DateLastModified, Len(CStr(f.DateLastModified)) - 3)
            Form1.ListView1.ListItems(lindex).SubItems(5) = f.ParentFolder
            Form1.ListView1.ListItems(lindex).SubItems(6) = f.Type
            lindex = lindex + 1
        Loop Until (FindNextFile(lFindFile, ff) = 0)
        FindClose lFindFile
        If Pflag Then Exit Sub
    End If
    findhandle = FindFirstFile(cPath & "*.*", xf)
    DoEvents
    Do
        If (xf.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
            If Asc(xf.cFileName) <> Asc(".") Then
                Dstr = cPath + Left(xf.cFileName, InStr(xf.cFileName, Chr(0)) - 1)
                FindFile Dstr, sFile
            End If
        End If
        If Pflag Then
            FindClose findhandle
            Exit Sub
        End If
    Loop Until (FindNextFile(findhandle, xf) = 0)
    FindClose findhandle
End Sub
 

顶一下
2
100%
踩一下
0
0%
评论列表 发表评论
推荐文章