于振海网搜索

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

发布时间:2011年11月21日
作者:未知  文章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文件模块
  • 【推荐文章】

  • 袁凯《白燕》赏析

    【原文】 白燕 袁凯① 故国飘零事已非,旧时王谢应见稀。 月明汉水初无影,雪满梁园...

  • 苏轼《六幺令·天中节》注释、译文、赏析

    【原文】 六幺令 天中节 苏轼 虎符缠臂,佳节又端午。门前艾蒲青翠,天淡纸鸢舞。粽...

  • 郎士元《柏林寺南望》赏析

    【原文】 柏林寺南望 郎士元 溪上遥闻精舍钟,泊舟微径度深松。 青山霁后云犹在,画...

  • 温庭筠《赠少年》赏析

    【原文】 赠少年 【唐】温庭筠 江海相逢客恨多,秋风叶下洞庭波。 酒酣夜别淮阴市,...

  • 柳宗元《衡阳与梦得分路赠别》注释、译文、赏

    【原文】 衡阳与梦得分路赠别 柳宗元 十年憔悴到秦京,谁料翻为岭外行。 伏波①故道...

  • 运用成语也要注意“性别”

    运用成语也要注意 性别 李占方 人有性别,成语也有 性别 。因此,我们在运用成语时,...

  • 【最新文章】

  • 袁凯《白燕》赏析
  • 苏轼《六幺令·天中节》注释、译文、赏析
  • 郎士元《柏林寺南望》赏析
  • 温庭筠《赠少年》赏析
  • 正月二十日往歧亭,郡人潘、古、郭三人送余于
  • 张炎《高阳台·西湖春感》赏析
  • 蒋捷《贺新郎》译文
  • 【唐】韩偓《草书屏风》翻译
  • 柳宗元《衡阳与梦得分路赠别》注释、译文、赏
  • 韩愈喊你去春游
  • 词句巧积累——房屋院落
  • 运用成语也要注意“性别”
  • 别把小事无限放大
  • 排好序 用准词
  • 学写采访记录