Below is a rather simple function which counts how many files are on a machine. Called on "C:\", it takes about 5 seconds to run. Unless I haven't run it in a while or first run a ram-clearing program, in which case it takes 60 seconds or more. I wouldn't have thought it could be caching since I'm doing a new scan each time (i.e. starting a new intance of the program, since all it does is this scan), but perhaps it relates to memory allocation? Any ideas on how to make that fast run happen every time, or on why it can't be done? Other programs (e.g. SpaceMonger) manage to get a total count of files in 10s even when I clear my ram or wait a long time between runs. So, there is definitely a way to do this, though not necessarily in VB.
Private Function countFiles(Name As String) As Long
On Error GoTo ErrorHandler
DoEvents
Const CurMthd = "countFiles"
Dim retval As Long
13 Dim FindData As win.WIN32_FIND_DATA
14 Dim SearchPath As String
15 Dim FileName As String
17 Dim SearchHandle As Long
If Right(Name, 1) <> "\" Then Name = Name & "\"
19 SearchPath = Name & "*.*"
20 SearchHandle = win.FindFirstFile(SearchPath, FindData)
Do
DoEvents
' g_Cancel = True
If g_Cancel Then
countFiles = retval
Exit Function
End If
22 If SearchHandle = win.INVALID_HANDLE_VALUE Or SearchHandle = ERROR_NO_MORE_FILES Then Exit Do
23 FileName = dsMain.RetainedStrFromPtrA(VarPtr(FindData.cFileName(0)))
24 If AscW(FileName) <> 46 Then
If (FindData.dwFileAttributes And win.FILE_ATTRIBUTE_DIRECTORY) Then
retval = retval + countFiles(Name & FileName)
Else
retval = retval + 1
End If
28 End If
29 Loop Until win.FindNextFile(SearchHandle, FindData) = 0
win.FindClose SearchHandle
countFiles = retval
Exit Function
ErrorHandler:
Debug.Print "Oops: " & Erl & ":" & Err.Description
Resume Next
End Function