于振海网——于振海的个人博客搜索

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

发布时间:2011年11月21日  更新时间:2024年6月17日
作者:未知  文章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
 

发表评论  挑错  文章投稿  联系我
  • 上一篇:Mysql:tinytext, text, mediumtext, longtext最大长度
  • 下一篇:vb读写ini文件模块
  • 【推荐文章】

  • 如何写好日记之每日生活新鲜事

    【原文】 如何写好日记之每日生活新鲜事 每一天,我们身边都会发生大大小小的事,它...

  • md5相同但内容不同的两个图片文件

    plane.jpg CRC-32:7fd01bda MD5 Hash:253dd04e87492e4fc3471de5e776bc3d SHA1 Hash...

  • 王禹偁《村行》译文、赏析

    【原文】 村行 王禹偁(chēng) 马穿山径菊初黄,信马悠悠野兴①长。 万壑有声含晚...

  • 妈妈的长发

    我爱美的妈妈, 有一头多么漂亮的长发! 她经常对着镜子梳啊梳, 呵呵,像个自恋的傻...

  • 清明节可以成为“我们的节日”

    清明临近,合肥市文明办提出将在全市开展我们的节日清明节主题活动。此消息一出,有...

  • 苏轼《西江月》注释、译文、赏析

    【原文】 西江月① 苏轼 照野弥弥浅浪,横空隐隐层霄。障泥②未解玉骢骄,我欲醉眠芳...

  • 【最新文章】

  • 如何写好作文之想象篇
  • 如何写好日记之人物影像及时摄
  • 如何写好日记之每日生活新鲜事
  • 如何教孩子诚实
  • 五讲四美三热爱
  • 中国软件大王与他的山村母亲
  • 李小云《风景》赏析
  • 电动车涨刹随动刹的刹车原理
  • 手机对孩子危害有多大?十年后的结果令人震惊
  • 西篱《怀念花溪》原文、赏析
  • md5相同但内容不同的两个图片文件
  • 王禹偁《村行》译文、赏析
  • 幼儿园数字描红学写数字A4打印-0
  • 蟋蟀鸣时易感秋
  • 光阴