Hey guys,
Im trying to count certain files that meet my criteria recursively.
I have this function that does find files that way, but now they won't be found when my criteria is set.
I am pretty sure that the function DID find found files that met my criteria, however it seemed it stopped working for some reason.
So if i let the function look for files below the directory "Subdirectory" with a .pdf extension it will show howmany files there are.
But, when i do the same and try to look for files with the current date into it (example as shown below) , it returns empty.
I am struggeling with this badly, can someone please rescue me !
Code:
Option Compare Database
Public Function RecursiveDir(colfiles As Collection, _
strFolder As String, _
strFileSpec As String, _
bIncludeSubfolders As Boolean)
Dim strTemp As String
Dim colFolders As New Collection
Dim vFolderName As Variant
'Add files in strFolder matching strFileSpec to colFiles
strFolder = TrailingSlash(strFolder)
strTemp = Dir(strFolder & strFileSpec)
Do While strTemp <> vbNullString
colfiles.Add strFolder & strTemp
strTemp = Dir
Loop
If bIncludeSubfolders Then
'Fill colFolders with list of subdirectories of strFolder
strTemp = Dir(strFolder, vbDirectory)
Do While strTemp <> vbNullString
If (strTemp <> ".") And (strTemp <> "..") Then
If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
colFolders.Add strTemp
End If
End If
strTemp = Dir
Loop
'Call RecursiveDir for each subfolder in colFolders
For Each vFolderName In colFolders
Call RecursiveDir(colfiles, strFolder & vFolderName, strFileSpec, True)
Next vFolderName
End If
End Function
Public Function TrailingSlash(strFolder As String) As String
If Len(strFolder) > 0 Then
If Right(strFolder, 1) = "\" Then
TrailingSlash = strFolder
Else
TrailingSlash = strFolder & "\"
End If
End If
End Function
Code:
Dim colfiles As New Collection
RecursiveDir colfiles, "I:\MyRootDIr\Subdirectory", "" & Format(Date, "dd/mm/yyy") & "*.pdf", True
Dim vFile As Variant
For Each vFile In colfiles
Next vFile
myControl = Colfiles.Count