使用FSO,相关代码在网上可以找到。

解决方案 »

  1.   

    Option ExplicitPrivate Const INVALID_HANDLE_VALUE = -1
    Private Const MAX_PATH = 256
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10Private Type FILETIME
       dwLowDateTime As Long
       dwHighDateTime As Long
    End TypePrivate Type SYSTEMTIME
      wYear As Integer
      wMonth As Integer
      wDayOfWeek As Integer
      wDay As Integer
      wHour As Integer
      wMinute As Integer
      wSecond As Integer
      wMilliseconds As Integer
    End TypePrivate 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 TypePrivate 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 LongPrivate Sub FindAll(ByVal strRootFolder As String)
    On Error GoTo ErrTrap
        
        Dim lngSearchHandle     As Long
        
        Dim lngRet              As Long    Dim strTemp             As String
        
        Dim FileNm              As String    Dim tFindData           As WIN32_FIND_DATA
        
        If Right$(strRootFolder, 1) <> "\" Then strRootFolder = strRootFolder & "\"
        
        lngSearchHandle = FindFirstFile(strRootFolder & "*.*", tFindData)
        
        If lngSearchHandle = INVALID_HANDLE_VALUE Then GoTo WayOut
        
        lngRet = 1
        
        Do While lngRet <> 0
            
            DoEvents
            
            strTemp = TrimNulls(tFindData.cFileName)
            Me.Caption = strTemp
            If (tFindData.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) = FILE_ATTRIBUTE_DIRECTORY Then
            
                If strTemp <> "." And strTemp <> ".." Then
                    Call FindAll(strRootFolder & strTemp)
                End If
            Else
                
                FileNm = strRootFolder & strTemp            '这里进行文件名的判断:
                If InStr(1, FileNm, ".exe") > 0 Then
                    Debug.Print FileNm
                End If
                        
            End If
                    
            lngRet = FindNextFile(lngSearchHandle, tFindData)
            DoEvents
            
        Loop
            Call FindClose(lngSearchHandle)
        
        GoTo WayOut
    ErrTrap:
        Select Case Err.Number
            Case 0
            Case Else
                MsgBox Err.Number & vbCrLf & Err.Description, , "FindAllDirFiles"
        End Select
    WayOut:
    End SubPrivate Sub Command1_Click()
        FindAll "c:"
    End SubPrivate Function TrimNulls(strString As String) As String
       Dim l As Long
       l = InStr(1, strString, Chr(0))
       If l = 1 Then
          TrimNulls = ""
       ElseIf l > 0 Then
          TrimNulls = Left$(strString, l - 1)
       Else
          TrimNulls = strString
       End If
    End Function
      

  2.   

    从别处找来的一篇文章看看是不是你想要的:以下代码演示了如何用Windows API函数遍历指定驱动器、目录的所有文件。其思路是:调出浏览文件夹窗口让用户指定所要搜索的起始路径,然后用查找文件的API函数遍历该目录下及其包含的子目录下的所有文件。本例需要:一个按钮,一个TextBox和一个ListBox,其中,TextBox应设置为多行。
    核心代码参照API-Guide的两个例子程序,特此声明。Option Explicit'查找第一个文件的API
    Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    '查找下一个文件的API
    Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    '获取文件属性的API
    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    '关闭查找文件的API
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    '以下为调用浏览文件夹窗口的API
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    Private Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long'常量
    Const MAX_PATH = 260
    Const MAXDWORD = &HFFFF
    Const INVALID_HANDLE_VALUE = -1
    Const FILE_ATTRIBUTE_ARCHIVE = &H20
    Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Const FILE_ATTRIBUTE_HIDDEN = &H2
    Const FILE_ATTRIBUTE_NORMAL = &H80
    Const FILE_ATTRIBUTE_READONLY = &H1
    Const FILE_ATTRIBUTE_SYSTEM = &H4
    Const FILE_ATTRIBUTE_TEMPORARY = &H100
    Const BIF_RETURNONLYFSDIRS = 1
    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'定义类(用于浏览文件夹窗口)
    Private Type BrowseInfo
    hWndOwner As Long
    pIDLRoot As Long
    pszDisplayName As Long
    lpszTitle As Long
    ulFlags As Long
    lpfnCallback As Long
    lParam As Long
    iImage As Long
    End Type'自定义函数
    Function StripNulls(OriginalStr As String) As String
    If (InStr(OriginalStr, Chr(0)) > 0) Then
    OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
    End Function'自定义函数
    Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, _
    DirCount As Integer)
    Dim FileName As String ' 文件名
    Dim DirName As String ' 子目录名
    Dim dirNames() As String ' 目录数组
    Dim nDir As Integer ' 当前路径的目录数
    Dim i As Integer ' 循环计数器变量
    Dim hSearch As Long ' 搜索句柄变量
    Dim WFD As WIN32_FIND_DATA
    Dim Cont As Integer
    If Right(path, 1) <> "\" Then path = path & "\"
    '搜索子目录
    nDir = 0
    ReDim dirNames(nDir)
    Cont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
    Do While Cont
    DirName = StripNulls(WFD.cFileName)
    If (DirName <> ".") And (DirName <> "..") Then
    If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
    dirNames(nDir) = DirName
    DirCount = DirCount + 1
    nDir = nDir + 1
    ReDim Preserve dirNames(nDir)
    End If
    End If
    Cont = FindNextFile(hSearch, WFD) '获取下一个子目录
    Loop
    Cont = FindClose(hSearch)
    End If
    ' 遍历目录并累计文件总数
    hSearch = FindFirstFile(path & SearchStr, WFD)
    Cont = True
    If hSearch <> INVALID_HANDLE_VALUE Then
    While Cont
    FileName = StripNulls(WFD.cFileName)
    If (FileName <> ".") And (FileName <> "..") Then
    FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
    FileCount = FileCount + 1
    List1.AddItem path & FileName
    End If
    Cont = FindNextFile(hSearch, WFD) ' 获取下一个文件
    Wend
    Cont = FindClose(hSearch)
    End If
    '如果子目录存在则遍历之
    If nDir > 0 Then
    For i = 0 To nDir - 1
    FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", _
    SearchStr, FileCount, DirCount)
    Next i
    End If
    End Function'查找按钮代码
    Sub Command1_Click()
    Dim SearchPath As String, FindStr As String
    Dim FileSize As Long
    Dim NumFiles As Integer, NumDirs As Integer
    Dim iNull As Integer, lpIDList As Long, lResult As Long
    Dim sPath As String, udtBI As BrowseInfo
    With udtBI
    '设置浏览窗口
    .hWndOwner = Me.hWnd
    '返回选中的目录
    .ulFlags = BIF_RETURNONLYFSDIRS
    End With'调出浏览窗口
    lpIDList = SHBrowseForFolder(udtBI)
    If lpIDList Then
    sPath = String$(MAX_PATH, 0)
    '获取路径
    SHGetPathFromIDList lpIDList, sPath
    '释放内存
    CoTaskMemFree lpIDList
    iNull = InStr(sPath, vbNullChar)
    If iNull Then
    sPath = Left$(sPath, iNull - 1)
    End If
    End IfScreen.MousePointer = vbHourglass
    List1.Clear
    SearchPath = sPath '选中的目录为搜索的起始路径
    FindStr = "*.*" '搜索所有类型的文件(此处可另作定义)
    FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    Text1.Text = "查找到的文件数:" & NumFiles & vbCrLf & "查找的目录数:" & _
    NumDirs + 1 & vbCrLf & "文件大小总共为:" & vbCrLf & _
    Format(FileSize, "#,###,###,##0") & "字节"
    Screen.MousePointer = vbDefault
    End Sub  
      

  3.   

    以下代码用FileSystemObject对像,利用递归调用搜索整个硬盘的DLL文件。
    Option Explicit
    Dim mfCancel As Boolean  '是否单击的“停止”按钮
    Dim mFSO As FileSystemObject
    Private Sub cmdSearch_Click()
        Dim foPath As String
        Dim fo As Folder
        Dim f As File
        Dim dr As Drive
        Label1.Caption = "正在搜索..."
        DoEvents
        mfCancel = False
        cmdSearch.Enabled = False  '搜索按钮无效
        cmdStop.Enabled = True  '停止按钮有效
        Set mFSO = New FileSystemObject
       List1.clear
        For Each dr In mFSO.Drives
            If mfCancel Then   '如果用户单击的“停止”按钮则停止搜索
                Exit For
            End If
            If dr.DriveType = 2 Then  '如果是硬盘就进行搜索
                foPath = dr.Path
                If Right(foPath, 1) = "\" Then
                Else
                    foPath = foPath & "\"
                End If
                subGetFolder foPath
            End If
        Next
        Label1.Caption = "找到" & ListView1.ListItems.Count & "个文件。"
        cmdStop.Enabled = False  '停止按钮无效
        cmdSearch.Enabled = True  '搜索按钮有效
        Set fo = Nothing
        Set f = Nothing
        Set dr = Nothing
        Set mFSO = Nothing
    End SubPrivate Sub subGetFolder(ByVal prmFoPath As String) '获取指定目录中的文件夹
        Dim fo As Folder
        Dim tFolder As Folder
        If Not mfCancel Then
            Set tFolder = mFSO.GetFolder(prmFoPath)
            Label1.Caption = "正在查找 " & prmFoPath   '用于显示当前正在搜索的文件夹
            DoEvents
            subGetFile prmFoPath
            For Each fo In tFolder.SubFolders
                If mfCancel Then  '用户单击的停止按钮则停止搜索
                    Exit For
                End If
                subGetFolder fo.Path  '递归调用
            Next
        End If
        
        Set fo = Nothing
        Set tFolder = Nothing
        
        
    End Sub
    Private Sub subGetFile(prmFoPath As String)  '获取指定目录中的文件
        Dim fListItem As ListItem
        Dim f As File
        Dim tFolder As Folder
        Set tFolder = mFSO.GetFolder(prmFoPath)
        For Each f In tFolder.Files
            If Len(f.Name) > 4 Then
                If UCase(Right(f.Name, 4)) = ".DLL" Then
                    List1.Additem f.Path
                End If
            End If
            If mfCancel Then
                Exit For
            End If
            DoEvents
        Next
        Set fListItem = Nothing
        Set f = Nothing
        Set tFolder = Nothing
    End SubPrivate Sub cmdStop_Click()
        mfCancel = True
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        End
    End Sub