Results 1 to 4 of 4

Thread: Getting list of files

  1. #1
    Guest
    My program involves getting a large list of files from a specified directory.
    Does anybody know which is the fastest among API, Dir function,File system object ?

    Thanks.

  2. #2
    New Member
    Join Date
    Jun 2000
    Posts
    2
    I believe that API should be the fastest.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    And FSO is slowest
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    If you're looking for an API routine for getting a list of Files, then here's some code I put together that works very fast:

    In a Module:
    Code:
    Option Explicit
    
    'API Consts, Types and Functions
    Private Const MAX_PATH = 260
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    
    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 Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    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 aFileList As Variant
    Private nFileCount As Long
    
    Public Function FastFindFiles(ByVal sFolder As String, Optional ByVal sPattern As String = "*") As Variant
        'Initialize the Private File Array and Count, then call the Fast
        'File Recursive Function to populate the Array, then return it.
        nFileCount = 0
        aFileList = Array()
        Screen.MousePointer = vbArrowHourglass
        Call RecurseFindFiles(sFolder, sPattern)
        Screen.MousePointer = vbDefault
        FastFindFiles = aFileList
    End Function
    
    Private Sub RecurseFindFiles(ByVal sFolder As String, ByVal sPattern As String)
        Dim tFD As WIN32_FIND_DATA
        Dim lFile As Long
        Dim bFound As Long
        Dim aSubs() As String
        Dim nSubs As Long
        Dim sFilename As String
        
        'Make sure the passed folder includes an ending "\"
        If Right(sFolder, 1) <> "\" Then sFolder = sFolder & "\"
        
        'Find the First File in the Specified Location
        lFile = FindFirstFile(sFolder & "*", tFD)
        bFound = lFile
        
        'Loop while a File is found
        While bFound
            'Get the Filename
            sFilename = UCase(Left(tFD.cFileName, InStr(tFD.cFileName, Chr(0)) - 1))
            If (tFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
                'If it's a Folder, add it to the Sub Folders Array
                If Left(sFilename, 1) <> "." Then
                    ReDim Preserve aSubs(nSubs)
                    aSubs(nSubs) = sFilename
                    nSubs = nSubs + 1
                End If
            Else
                'If it's a File, compare it to the Pattern for a Match
                If sFilename Like UCase(sPattern) Then
                    'If it matches, add it to the File Array
                    ReDim Preserve aFileList(nFileCount)
                    aFileList(nFileCount) = sFolder & sFilename
                    nFileCount = nFileCount + 1
                End If
            End If
            'Find the Next File, (if there is one).
            bFound = FindNextFile(lFile, tFD)
        Wend
        'Close the API Find Handle
        Call FindClose(lFile)
        
        'If there were Sub Folders found, Recurse them too..
        If nSubs Then
            For nSubs = 0 To UBound(aSubs)
                Call RecurseFindFiles(sFolder & aSubs(nSubs), sPattern)
            Next
        End If
    End Sub
    Example:
    Code:
    Private Sub Command1_Click()
        Dim vFiles As Variant
        Dim nFile As Long
        
        vFiles = FastFindFiles("C:\", "*.txt")
        For nFile = 0 To UBound(vFiles)
            List1.AddItem vFiles(nFile)
        Next
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width