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.
Locate the file or folder whose permission changes you wish to track. Right click on it and go to Properties. In the Security tab, click the Advanced button. In Advanced Security Settings for Active Directory window, go to Auditing tab, and click the Add button to add a new auditing entry.
Type the path to the folder that you are monitoring. You can use the ellipsis (...) button to browse for the folder. Select this option to monitor the files and folders in sub-folders in the Folder that you specified.
Use a FileSystemWatcher like below to create a WatcherCreated Event().
I used this to create a Windows Service that watches a Network folder and then emails a specified group on arrival of new files.
// Declare a new FILESYSTEMWATCHER
protected FileSystemWatcher watcher;
string pathToFolder = @"YourDesired Path Here";
// Initialize the New FILESYSTEMWATCHER
watcher = new FileSystemWatcher {Path = pathToFolder, IncludeSubdirectories = true, Filter = "*.*"};
watcher.EnableRaisingEvents = true;
watcher.Created += new FileSystemEventHandler(WatcherCreated);
void WatcherCreated(object source , FileSystemEventArgs e)
{
//Code goes here for when a new file is detected
}
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.
If you want something non-programmatic try GiPo@FileUtilities ... but in that case the question wouldn't belong here!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With