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

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

  • 辛弃疾《清平乐·题上卢桥》创作背景、赏析

    【原文】 清平乐① 题上卢桥② 辛弃疾 清泉犇快,不管青山碍③。千里盘盘平世界,更...

  • 苏轼《澄迈驿通潮阁二首》赏析

    【原文】 澄迈驿通潮阁二首① 苏轼 (一) 倦客愁闻归路遥, 眼明飞阁俯长桥。 贪看...

  • 如何写好议论文——论题:奋斗

    【论题阐述】 奋斗 一词的本义就是指为了目标去努力行动。在现代社会中,面临着很大...

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

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

  • 海王星上为什么风暴不断

    在我们的心目中,风暴多起源于太阳的热力:阳光使大气和海水受热,造成空气对流而刮...

  • 站成一棵树

    父亲是在李阳出事的第二天赶回村庄的。 事情也不算大,李阳打了同学。李阳以前也打同...

  • 【最新文章】

  • 白居易《江楼夕望招客》赏析
  • 姜夔《点绛唇·丁未冬过吴松作》赏析
  • 辛弃疾《清平乐·题上卢桥》创作背景、赏析
  • 陶渊明《答庞参军》赏析
  • 苏轼《澄迈驿通潮阁二首》赏析
  • 垃圾分类的作文素材
  • 如何写作文——一次奇特的比赛(想象作文)
  • 如何培养孩子独自人格,让他学会承担自己的人
  • 如何写好议论文——论题:奋斗
  • 如何写好作文之写人篇
  • 如何写好作文之叙事篇
  • 如何写好作文之写景篇
  • 如何写好日记之今天我来画美景
  • 如何写好日记之每日生活新鲜事
  • 如何写好日记之斑斓校园每一天