Counting Files in a Directory...
Posted on 2004-08-18
Situation: I have 2 folders in C:/... f1 called ScannedClientDocs (all files of interest are scanned straight in. i.e., NO subfolders) and f2 named ClientDocs which is further broken down into sub-folders where eventually the .jpg resides. I want to compare the # of .jpg images in each. I know the following will correctly give me a count for f1:
'get the number of files by calling a user function
I = CountFiles("C:\ScannedClientDocs") 'Scanned
'display the results in a message box
MsgBox "There are " & I & _
" jpg files in the directory you specified"
Function CountFiles(tgtDir As String) As Integer
Dim fName As String
'Retrieve the first entry, handle error if directory not found
On Error GoTo badDirectory
fName = Dir(tgtDir & "\*.jpg")
On Error GoTo 0
'loop through all files in the directory and increment the function's value
Do While fName <> ""
' Ignore the current directory and
' the encompassing directory.
If fName <> "." And fName <> ".." Then
CountFiles = CountFiles + 1
' Get next entry.
fName = Dir()
'come here if directory can not be accessed
MsgBox "The directory you specified does not exist or " & _
"can not be accessed. Activity halted."
How do I get this routine (or reasonable facsimile thereof!) to get into the "bowels" (i.e., sub folder of sub folder) of ClientDocs so as to count the .jpg files?
I believe this can be done since if you right click the ClientDocs folder in C it tells me the # of files AND folders present.