Results 1 to 4 of 4

Thread: check and advise please

  1. #1

    Thread Starter
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    check and advise please

    i have tried to cut down the findfirst, findnext api for finding all files and subfolder files in a given path, i have cut it down to the bare minimum required because all the examples i see have loads of other stuff with them. can you check over it and see if i have missed anything or where it could be better please.
    VB Code:
    1. Option Explicit
    2. Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
    3. Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
    4. Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
    5. Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
    6.  
    7. Const FILE_ATTRIBUTE_DIRECTORY = &H10
    8.  
    9. Private Type FILETIME
    10.   dwLowDateTime As Long
    11.   dwHighDateTime As Long
    12. End Type
    13.  
    14. Private Type WIN32_FIND_DATA
    15.   dwFileAttributes As Long
    16.   ftCreationTime As FILETIME
    17.   ftLastAccessTime As FILETIME
    18.   ftLastWriteTime As FILETIME
    19.   nFileSizeHigh As Long
    20.   nFileSizeLow As Long
    21.   dwReserved0 As Long
    22.   dwReserved1 As Long
    23.   cFileName As String * 260
    24.   cAlternate As String * 14
    25. End Type
    and the sub
    VB Code:
    1. Private Sub showallfiles(spath As String)
    2. Dim sdir As String, dirarr() As String, x As Integer
    3. Dim i As Integer, sh As Long, OK As Boolean
    4. Dim wfd As WIN32_FIND_DATA
    5.    
    6.     If Right$(spath, 1) <> "\" Then spath = spath & "\"
    7.     OK = True
    8.    
    9.     sh = FindFirstFile(spath & "*.*", wfd)
    10.     If sh <> -1 Then
    11.         Do While OK
    12.           sdir = Left$(wfd.cFileName, InStr(wfd.cFileName, Chr(0)) - 1)
    13.             If (sdir <> ".") And (sdir <> "..") Then
    14.               If GetFileAttributes(spath & sdir) And FILE_ATTRIBUTE_DIRECTORY Then
    15.                 ReDim Preserve dirarr(x)
    16.                 dirarr(x) = spath & sdir & "\"
    17.                 x = x + 1
    18.               Else
    19.                List1.AddItem spath & sdir
    20.               End If
    21.             End If
    22.           OK = FindNextFile(sh, wfd)
    23.         Loop
    24.           FindClose sh
    25.     End If
    26.        
    27.    For i = 0 To x - 1
    28.     showallfiles dirarr(i)
    29.    Next i
    30. End Sub
    thank you.
    casey.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: check and advise please

    i pasted your code into an open project and it worked fine,

    to check the number of files returned i added

    Debug.Print List1.ListCount
    immediately before your end sub

    expecting it to return the number of files in the list box one time on completion, however it actually printed to the immediate window 86 times, so is probably to do with number of folders, but the file count at the bottom was right

    rgds pete

  3. #3

    Thread Starter
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016

    Re: check and advise please

    thanks westconn1.

    do you or anybody else know if there is something missing from that sub using them api because i was under the impression that these api made the search for all files a lot faster but i am having the same results if not slightly faster using the Dir() function.

    thank you.
    casey.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: check and advise please

    Casey,

    i presume if something was missing it would fail to work, not just go slower, how many files are you trying to return in your test, and are you getting from subdirectories as well.

    i wasn't timing it when i tested, but liked it because it was very easy to get the subdirectorys as well, i thought it was fast enough returning 788 files in 86 folders

    i think sometimes apis are no faster than VB, just they can do more things

    rgds p.

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