Visual Basic Code of the Week (COTW)
Issue #131
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 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.

Issue topic: Monitoring Folders for changes

Extra CASH! and a new Newsletter

We are going to create a new VB Tips newsletter. There will be no fee for this newsletter. It will contain short (10-50 lines of text) code snippets, tips, and other useful information for Visual Basic. If you have any tips to contribute, email us at Be sure to include instructions and source code. For each tip received which gets published we will pay you $10 to $50 US Dollars depending on length and complexity.

If you are interested in subscribing to this newsletter just send an email to and you will be added to our subscriber list.


In this Issue

This issue introduces a class to monitor any file changes made in a particular folder.

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


This class is very useful when any monitoring of folders is necessary. It will automatically notify the calling program when a file changes in a folder. It is most useful for applications which require folders to be updated when external changes are made to them. It is also very useful for batch file processing where an application needs to process a file once it appears in a particular folder.

This class can not tell what kind of change has occurred, but only that some change occurred in the folder specified by the FolderToWatch property. Additional code would have to be written to determine this information. It does not seem to be available from the Windows API.


Public Enum FileNotifyFlags

This enumerator specifies which events can be monitored. Most of the constants are pretty obvious. For full details on each one of these properties see at Microsoft's web site. Each constant can be OR'd together to watch several events.

Public FolderToWatch As String

This is the folder name to watch, such as e:\temp or c:\windows\system

Public Event FolderChanged(sFolder As String, eFlags As FileNotifyFlags)

This event will be raised everytime a change in detected in the FolderToWatch. Which changes are monitored is determined by the eFlags option on the Monitor method.


Public Sub Monitor(Optional eFlags As FileNotifyFlags = FileNotifyDefault)

Starts the monitoring process. The Monitor routine will not return until the Abort method is called or some error occurs. Typically this call will be put behind a button on a form or a timer on a form to keep the main application from hanging up. A future version of this class will have an asynchronous version of this routine.

Public Sub Abort()

Cancels the monitoring process. It is important to call this before exiting your program if the Monitor method has already been called. If you do not call this your program might not clean up all its memory correctly.

Sample Usage

This sample shows how to use the cFileNotify class. This sample assumes a form has been created and there are two buttons on the form called cmdBegin and cmdEnd and one listbox called List1. This sample will monitor the e:\temp\test folder for changes once the begin button is clicked. When a change is detected an event will be raised (Note the WithEvents option in the Dim statement) and a line added to the listbox.

Dim WithEvents oFileNotify As cFileNotify

Private Sub cmdBegin_Click()
    Set oFileNotify = New cFileNotify
    oFileNotify.FolderToWatch = "e:\temp\test"
End Sub

Private Sub cmdEnd_Click()
End Sub

Private Sub oFileNotify_FolderChanged(sFolder As String, eFlags As FileNotifyFlags)
    List1.AddItem Now & " " & sFolder & " " & eFlags
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