ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Faster alternative to Dir() ? (https://www.excelbanter.com/excel-programming/304445-faster-alternative-dir.html)

R Avery

Faster alternative to Dir() ?
 
I have a program that requires parsing a very large directory structure,
and although the Dir() function is MUCH better than using the
scripting.filesystemobject for iterating over file paths, I was
wondering if there is an even faster alternative, perhaps API calls.

All I need to be able to do is to recursively enumerate all file paths
within a directory. I am doing this from Excel 2002 VBA. Any help
would be most appreciated.

R Avery

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


All times are GMT +1. The time now is 10:21 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com