View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
R Avery R Avery is offline
external usenet poster
 
Posts: 220
Default Faster alternative to Dir() ?

I have found API functions to iterate over files on the following site:
http://support.microsoft.com/default...b;en-us;185476

However, these API functions are significantly slower than using Dir().
The exact functions I used are at the bottom. I iterated over a
particular directory 20 times each. These API functions are not what I
am looking for: are there any other possibilities?



These are my results:
API
0.746 seconds elapsed.
GetFileListArray
0.035 seconds elapsed.







Function FindFilesAPI(ByVal Path As String, Optional ByVal SearchStr As
String = "*") As String()
Dim FileName As String ' Walking filename variable...
Dim fileNames() As String ' Buffer for directory name entries
Dim nFile As Integer ' Number of directories in this path
Dim hSearch As Long ' Search Handle
Dim WFD As WIN32_FIND_DATA
Dim Cont As Integer

If Right(Path, 1) < "\" Then Path = Path & "\"

' 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 < "..") And _
((GetFileAttributes(Path & FileName) And _
FILE_ATTRIBUTE_DIRECTORY) < FILE_ATTRIBUTE_DIRECTORY) Then
ReDim Preserve fileNames(nFile)
fileNames(nFile) = FileName
nFile = nFile + 1

End If
Cont = FindNextFile(hSearch, WFD) ' Get next file
Wend
Cont = FindClose(hSearch)
End If

FindFilesAPI = fileNames
End Function





Public Function GetFileListArray(ByVal Path As String, Optional ByVal
Filter As String = "*") As String()
Dim DirectoryFiles() As String
Dim strFileName As String

strFileName = Dir(Path & Filter)
Do While strFileName < ""
If strFileName < "" Then
ReDim Preserve DirectoryFiles(Count)
DirectoryFiles(Count) = strFileName
Count = Count + 1
End If
strFileName = Dir()
Loop

GetFileListArray = DirectoryFiles
End Function