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 email@example.com. 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 firstname.lastname@example.org and you will be added to our subscriber list.
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 email@example.com
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 http://msdn.microsoft.com/library/psdk/winbase/filesio_9hgu.htm 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.
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" oFileNotify.Monitor End Sub Private Sub cmdEnd_Click() oFileNotify.Abort End Sub Private Sub oFileNotify_FolderChanged(sFolder As String, eFlags As FileNotifyFlags) List1.AddItem Now & " " & sFolder & " " & eFlags End Sub
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: http://www.codeoftheweek.com/membersonly/bi/0131.html