Link to home
Start Free TrialLog in
Avatar of ronaldj
ronaldj

asked on

Counting Files in a Directory...

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:

Sub Count_Files_In_A_Directory()
  Dim I
  '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"
End Sub

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
        End If
        ' Get next entry.
        fName = Dir()

Loop
    Exit Function
badDirectory:
    'come here if directory can not be accessed
    MsgBox "The directory you specified does not exist or " & _
            "can not be accessed.  Activity halted."
    End
End Function

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.

Thanks,
Ron
ASKER CERTIFIED SOLUTION
Avatar of Colosseo
Colosseo
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ronaldj
ronaldj

ASKER

Hey Scott,

Thanks for the response. Yeah, the directory structure is kinda wierd but not my idea. Here's what happens: Client scans documents required by regulator into one massive folder called ScannedClientDocs(pretty original, huh?) Example: MrXYZdd.jpg.

I wrote a vba routine that creates a general folder called ClientDocs; under the general folder a folder named MrXYZ is then created. Sub to this is a folder called DueDilligence (grabs dd from lookup function and uses long name, DueDilligence to name folder). In this folder the document MrXYZdd.jpg is then copied/stored. So now, from a UserForm, the client selects an account, program auto selects all docs associated with same, puts them into a temp folder which is then made accessible to MS Picture and Fax Viewer. Kinda neat. So much for the bacxkground...

Your response triggered an idea so here's what I tried...seems to do the trick:

Sub FindClientDocsJPGFiles()
Dim FS As Office.FileSearch
Dim vaFileNattle As Variant
Dim stMessage As String
Dim i As Long, iCount As Long

Set FS = Application.FileSearch

With FS
'Clear old search criteria
.NewSearch

'Directory to search
.LookIn = "C:\ClientDocs"

'Include sub folders in search
.SearchSubFolders = True

'Look for .jpg files
.filename = "*.jpg"
'.FileType = msoFileTypeAllFiles

'Doesn 't matter when last modified
.LastModified = msoLastModifiedAnyTime

'Carry out search and capture number of files found
iCount = .Execute

stMessage = Format(iCount, "0 ""Files Found""")
'List the files in the FoundFiles collection
For Each vaFileName In .FoundFiles
stMessage = stMessage '& vbCr & vaFileName
Next vaFileName

MsgBox stMessage

End With

End Sub

Obviously the msg lines are only there temp so I could see the result. Whatcha think?

Thanks again,
Ron
Hey Ron

Yeah my code was only an example of how I would do it using the filesystemobject.

Like you say and from what I understand of your code it should work fine. The .SearchSubFolders = True is perfect for what you need as this sends the code into the subfolders like my code so it looks like you have it sorted :)

Scott