Visual Basic Code of the Week (COTW)
Issue #81
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Get your Free Year of Code of the Week!!!

For the next month we are offering a free annual subscription to anyone that submits source code for an issue which gets published. If you have some useful source code that would be valuable for our members, fill out the form at

Please make sure all source code is tested and is in working condition. We are looking for all types of source from database access routines to API calls to graphics routines. We are not looking for one or two lines tricks at this time (this will be a future offering) . The source code should be at least 10 or 15 lines of long, preferably longer. It should be commented or at least readable by the average programmer.

If you are currently a member of Code of the Week we will extend your subscription an additional year.

Cool Software

Download a free copy of NetMon - Your Internet Performance Monitor at

In this Issue

In this issue we will extend the source code introduced in the last issue. This issue will show how to recurse an entire directory tree using the cFindFiles class. On a Pentium II 266Mhz machine it scanned about 1300 files in just over a second or two.

This source code is designed for VB 5.0 and up. Questions? Email us at


The cFindFiles class module was written by Tracy Martin with modifications by C&D Programming Corp. It contains a couple of methods for getting files and information about files it has found. Issue #80 documents nearly all of the properties and methods in this class. This week we add two properties, one method and one event.

If you have any questions about using this class, let us know at


Public Sub FindAllFiles(sDir As String, sFileMask As String)

This routine will scan an the directory specified in sDir (example would be c:\windows or c:\temp) and with a file mask of sFileMask (example would be *.doc or *.*). The difference between this routine and the FindFile routine is that this one will recurse sDir and all directories below sDir. As it finds a matching file it will raise the event FoundFile. This event is documented below.


Public Event FoundFile(fnd As cFindFiles)

The FoundFile event is triggered as each file is found in the FindAllFiles subroutine. Inside the FoundFile event you can access all properies of the cFindFiles for the file passed in fnd. You can perform functions like adding file information to a list box or list view, calculating how many matching files were found, or simply adding up the amount of disk space used by the matching files.



See each function/property for details.

Sample Usage

The below sample describes how to use the FindAllFiles feature of the cFindFiles class. It will show all filenames and the number of files located in the E:\TEMP directory. It assumes this code is located in a form and you have added a listbox called List1 to the form.

Option Explicit

' You need to define this variable like this so you can get the
' events from the cFindFiles class.
Dim WithEvents fnd As cFindFiles

Private Sub fnd_FoundFile(fnd As cFindFiles)
    List1.AddItem fnd.GetFilePath  ' Add the filename to the list box.
    ' If this was a ListView you can add other items like filesize and date/time.
    DoEvents   ' Give Windows some time to update.
End Sub

Private Sub Form_Load()
    ' We do this to allow events to pass into the above routine.
    Set fnd = New cFindFiles

    Call fnd.FindAllFiles("e:\temp", "*.*")
    MsgBox "Found " & List1.ListCount & " matching files."
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945