只要一个文本框,一个按钮,文本框里输入文件名,电击按钮就搜索整个硬盘里是否有指定文件,有的话显示在窗体上。

解决方案 »

  1.   

    用fso对象
      
    *****************************************************************************
    欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码) 
    http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
      

  2.   

    Public ScanFileName As String'搜索文件
    Public Sub ScanFile(ByVal Path As String, Optional ByVal Filter As String = "*.*")
        Dim FileName As String
        Dim Folder() As String
        Dim i As Long, x As Long
        Dim str As String
        Dim ext() As String, ext2 As String    Path = ValidateDir(Path)
        ext = Split(Filter, ";")    FileName = Dir(Path)
    '    Debug.Print FileName
        Do While FileName <> ""
            If InStr(1, Filter, "*.*") Then
                str = str & Path & FileName & vbCrLf
            Else
                ext2 = LCase(GetExtension(FileName))
                For i = 0 To UBound(ext)
                    If LCase(GetExtension(ext(i))) = ext2 Then
                        str = str & Path & FileName & vbCrLf
                        Exit For
                    End If
                Next        End If
            FileName = Dir
        Loop
        FileName = Dir(Path, vbDirectory)
        Do While FileName <> ""
            If FileName <> "." And FileName <> ".." Then
    '            MsgBox GetAttr(Path & FileName) And vbDirectory
                If GetAttr(Path & FileName) And vbDirectory Then
                    x = x + 1
                    ReDim Preserve Folder(x)
                    Folder(x) = Path & FileName
                End If
            End If
            FileName = Dir
            DoEvents
        Loop
        For i = 1 To x
            ScanFile Folder(i) & "\", Filter
        Next
        ScanFileName = ScanFileName & str
    End Sub
      

  3.   

    下面这个代码将遍历整个硬盘查找test.txt文件,并把所有找到的结果显示在list1中,注意窗体中别使用强制变量声明Option Explicit!模块中代码:
    Public Declare Function GetLogicalDrives Lib "kernel32" () As Long
    Public Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    Public Const INVALID_HANDLE_VALUE = -1
    Public Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    Public Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    Public Type FileTime
            dwLowDateTime As Long
            dwHighDateTime As Long
    End Type
    Public Const MaxLFNPath = 260
    Public 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 * MaxLFNPath
            cShortFileName As String * 14
    End Type
      

  4.   

    窗体中代码:
    Dim wfd As WIN32_FIND_DATA, hItem&, hFile&
    Const vbBackslash = "\"
    Const vbAllFiles = "*.*"
    Const vbKeyDot = 46
    Dim sFind As StringPrivate Sub Command1_Click()
        Me.MousePointer = 11
        sFind = "test.txt"  '要查找的文件名
        On Error Resume Next
        drvbitmask& = GetLogicalDrives()
        If drvbitmask& Then
            maxpwr% = Int(Log(drvbitmask&) / Log(2))
            For pwr% = 0 To maxpwr%
                If (2 ^ pwr% And drvbitmask&) Then
                    Call SearchDirs(Chr$(vbKeyA + pwr%) & ":\")
                End If
            Next
        End If
        Me.MousePointer = 0
        MsgBox "查找完毕"
    End SubPrivate Sub SearchDirs(curpath$)
        Dim dirs%, dirbuf$(), I%
        DoEvents
        hItem& = FindFirstFile(curpath$ + vbAllFiles, wfd)
        If hItem& <> INVALID_HANDLE_VALUE Then
            Do
                If (wfd.dwFileAttributes And vbDirectory) Then
                    If Asc(wfd.cFileName) <> vbKeyDot Then
                        TotalDirs% = TotalDirs% + 1
                        If (dirs% Mod 10) = 0 Then ReDim Preserve dirbuf$(dirs% + 10)
                        dirs% = dirs% + 1
                        dirbuf$(dirs%) = Left$(wfd.cFileName, InStr(wfd.cFileName, vbNullChar) - 1)
                    End If
                ElseIf Not UseFileSpec% Then
                    TotalFiles% = TotalFiles% + 1
                End If
            Loop While FindNextFile(hItem&, wfd)
            Call FindClose(hItem&)
        
        End If
        Call SearchFileSpec(curpath$)
        For I% = 1 To dirs%: SearchDirs curpath$ & dirbuf$(I%) & vbBackslash: Next I%
    End SubPrivate Sub SearchFileSpec(curpath$) '搜索文件
        hFile& = FindFirstFile(curpath$ & sFind, wfd)
        If hFile& <> INVALID_HANDLE_VALUE Then
            Do
                List1.AddItem curpath$ & Left$(wfd.cFileName, InStr(wfd.cFileName, vbNullChar) - 1)
                DoEvents
            Loop While FindNextFile(hFile&, wfd)
            Call FindClose(hFile&)
        End If
    End Sub
      

  5.   

    happy_sea的程序编译错误啊,ByRef参数类型不符。我找不出哪里错了,你再看看吧。
      

  6.   

    測試過開心海的代碼,完全可以正常運行
    也沒樓主所說的ByRef参数类型不符 錯誤