Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long

解决方案 »

  1.   

    用一个隐藏的dirlist控件,再循环取一下路径就可以了。
      

  2.   

    我也做过类似的,好像也只有用dir来判断
    用FileSystemObject对象不知道是不是可以提高效率
      

  3.   

    问:Jneu(沧海桑田) 
    WIN32_FIND_DATA是什么数据类型?它需要引用什么?可以给我一个例子吗?
    问:songyangk(小草) 
    隐藏的dirlist控件在那里?我怎么没有找到啊可以发一个给我吗?[email protected]
      

  4.   

    据说用控件,到最后还是转化成api函数的。
    api还是比较底层的啊
    so ,,,,推荐用api,呵呵,
      

  5.   

    API用那个啊?怎么用?谢谢...........
      

  6.   


    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 GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As LongConst 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 = &H100Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    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 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 FunctionFunction FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
        'KPD-Team 1999
        'E-Mail: [email protected]    Dim FileName As String ' Walking filename variable...
        Dim DirName As String ' SubDirectory Name
        Dim dirNames() As String ' Buffer for directory name entries
        Dim nDir As Integer ' Number of directories in this path
        Dim I As Integer ' For-loop counter...
        Dim hSearch As Long ' Search Handle
        Dim WFD As WIN32_FIND_DATA
        Dim Cont As Integer
        If Right(path, 1) <> "\" Then path = path & "\"
        ' Search for subdirectories.
        nDir = 0
        ReDim dirNames(nDir)
        Cont = True
        hSearch = FindFirstFile(path & "*", WFD)
        If hSearch <> INVALID_HANDLE_VALUE Then
            Do While Cont
            DirName = StripNulls(WFD.cFileName)
            ' Ignore the current and encompassing directories.
            If (DirName <> ".") And (DirName <> "..") Then
                ' Check for directory with bitwise comparison.
                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) 'Get next subdirectory.
            Loop
            Cont = FindClose(hSearch)
        End If
        ' Walk through this directory and sum file sizes.
        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) ' Get next file
            Wend
            Cont = FindClose(hSearch)
        End If
        ' If there are sub-directories...
        If nDir > 0 Then
            ' Recursively walk into them...
            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
        Screen.MousePointer = vbHourglass
        List1.Clear
        SearchPath = Text1.Text
        FindStr = Text2.Text
        FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
        Text3.Text = NumFiles & " Files found in " & NumDirs + 1 & " Directories"
        Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
        Screen.MousePointer = vbDefault
    End Sub
      

  7.   

    非常感谢Jneu(沧海桑田) 的代码!它是完全可以执行的。
    但是这个根本还是不符合要求啊。你哪个查找文件的方式是按对应的驱动器查找相对应的文件!
    就好象windows提供的查找文件方式一样。但是这样不是更慢了吗?你要知道我是从数据库中能读出15000条记录我只要查他国路径所指定的文件是否存在就行了!
    那应该怎么办才好呢?问大家~~
      

  8.   

    你的程序的瓶颈在于你读取数据库纪录的时间而不在于你判定文件是否存在的时候。BTW: 判定一个文件是否存在,使用FileSystemObject.FileExists就可以了。
    Function IsFileExist(filespec as string) as boolean
       Dim fso as Object
       Set fso = CreateObject("Scripting.FileSystemObject")
       If (fso.FileExists(filespec)) Then
          IsFileExist=true
       Else
          IsFileExist=false
       End If
       Set fso = nothing
    End Function
      

  9.   

    to :hydnoahark(诺亚方舟) 
        如果我查找的文件在本机,那速度非常快,这说明不是读取数据库纪录的时间太长的缘故!
    现在因为我查找的文件都在网络上,请问还有什么好的方法吗?
      

  10.   

    我也有这样的问题,用filesystemobject.fileexists查找本机的速度很快,若要查找局域网上的文件速度会慢一些,internet上就不清楚了,我有一万条记录,大约需要不到半分钟的时间,应该可以接受的了。但是如果没有连上网络,就必须先测试网络是否连接需要一些时间。