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

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文件模块
  • 【推荐文章】

  • 尊重别人就等于尊重自己

    一天下午,一位穿得很时髦的中年女人带着一个小男孩走进美国著名企业亚联集团总部大厦...

  • 王禹偁《官舍竹》赏析

    【原文】 官舍竹 王禹偁 谁种萧萧数百竿?伴吟偏称作闲官①。 不随夭艳争春色,独守...

  • 中国近代科学为什么落后了?

    中国古代有着灿烂的文明,是世界四大文明古国之一,在16世纪以前长达1千多年的时期内...

  • 我们为什么活着

    2008年华尔街金融风暴袭来,美国一家电器制造公司倒闭,公司总裁卡来尔来到多伦斯湖准...

  • Windows XP查看本机IP地址、MAC地址、网关、D

    点击【开始】,然后点击【运行】,在输入栏输入cmd三个字母,在打开的窗口中输入ipcon...

  • 机遇只偏爱有准备的头脑

    山重水复疑无路,柳暗花明又一村。 众里寻他千百度,蓦然回首,那人却在,灯火阑珊处...

  • 【最新文章】

  • vb读写ini文件模块
  • 致网虫儿子
  • 鸟是树的花朵
  • 小善大爱
  • 鲁山风光
  • 荆山寺
  • 尊重别人就等于尊重自己
  • 沂源灵芝洞
  • 沂源上崖洞与下崖洞
  • 沂源九天洞
  • 理想召唤行动
  • 汉字之美
  • 谷歌地球自己标注的地方,文件保存的目录
  • 四月的时候,星星点点的蒲公英便绽开了,它们
  • 狗道