When a change is made within a directory on a Windows system, I need a program to be notified immediately of the change.
Is there some way of executing a program when a change occurs?
I'm not a C/C++/.NET programmer, so if I could set up something so that the change could trigger a batch file then that would be ideal.
WatchDirectory automates all your file-based tasks! Just for example - you can tell watchDirectory to monitor a directory and automatically copy any new files to another location. Or perhaps have it compress (ZIP), email, upload or delete the files. Right click the Videos folder and select Properties. In Properties, go to the Location tab, and click on the Move button. In the folder browse dialog, select the new folder you want to store your Videos. Click on the OK button to make the change. When prompted, click on Yes to move all your files from the old location to the new folder.
FileSystemWatcher is the right answer except that it used to be that FileSystemWatcher only worked for a 'few' changes at a time. That was because of an operating system buffer. In practice whenever many small files are copied, the buffer that holds the filenames of the files changed is overrun. This buffer is not really the right way to keep track of recent changes, since the OS would have to stop writing when the buffer is full to prevent overruns.
Microsoft instead provides other facilities (EDIT: like change journals) to truly capture all changes. Which essentially are the facilities that backup systems use, and are complex on the events that are recorded. And are also poorly documented.
A simple test is to generate a big number of small files and see if they are all reported on by the FileSystemWatcher. If you have a problem, I suggest to sidestep the whole issue and scan the file system for changes at a timed interval.
I came on this page while searching for a way to monitor filesystem activity. I took Refracted Paladin's post and the FileSystemWatcher that he shared and wrote a quick-and-dirty working C# implementation:
To use this, download Visual Studio (Express will do). The create a new C# console application called Folderwatch and copy and paste my code into your Program.cs.
As an alternative you could use Sys Internals Process Monitor: Process Monitor It can monitor the file system and a bunch more.
This question helped me a lot to understand the File Watcher System. I implemented ReadDirectoryChangesW to monitor a directory and all its sub directories and get the information about change in those directories.
I have written a blog post on the same, and I would like to share it so it may help someone who land up here for the same problem.
Win32 File Watcher Api to monitor directory changes.
NehaNeha
Not the answer you're looking for? Browse other questions tagged windowsdirectorymonitoring or ask your own question.