|
-
Jan 14th, 2005, 04:38 PM
#1
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:
Option Explicit
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 Long
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 * 260
cAlternate As String * 14
End Type
and the sub
VB Code:
Private Sub showallfiles(spath As String)
Dim sdir As String, dirarr() As String, x As Integer
Dim i As Integer, sh As Long, OK As Boolean
Dim wfd As WIN32_FIND_DATA
If Right$(spath, 1) <> "\" Then spath = spath & "\"
OK = True
sh = FindFirstFile(spath & "*.*", wfd)
If sh <> -1 Then
Do While OK
sdir = Left$(wfd.cFileName, InStr(wfd.cFileName, Chr(0)) - 1)
If (sdir <> ".") And (sdir <> "..") Then
If GetFileAttributes(spath & sdir) And FILE_ATTRIBUTE_DIRECTORY Then
ReDim Preserve dirarr(x)
dirarr(x) = spath & sdir & "\"
x = x + 1
Else
List1.AddItem spath & sdir
End If
End If
OK = FindNextFile(sh, wfd)
Loop
FindClose sh
End If
For i = 0 To x - 1
showallfiles dirarr(i)
Next i
End Sub
thank you.
casey.
-
Jan 14th, 2005, 10:22 PM
#2
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
-
Jan 15th, 2005, 09:02 AM
#3
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.
-
Jan 15th, 2005, 09:24 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|