Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I monitor a Windows directory for changes?

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.

like image 316
Liam Avatar asked Apr 17 '09 15:04

Liam


People also ask

How do I monitor a folder for changes?

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.

How do I monitor a folder?

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.


3 Answers

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
    }
like image 68
Refracted Paladin Avatar answered Oct 12 '22 17:10

Refracted Paladin


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.

like image 34
Arturo Hernandez Avatar answered Oct 12 '22 19:10

Arturo Hernandez


If you want something non-programmatic try GiPo@FileUtilities ... but in that case the question wouldn't belong here!

like image 6
Rob Walker Avatar answered Oct 12 '22 19:10

Rob Walker