- ReadDirectoryChangesW; and
- AutoIt3 3.2 or higher
_FileSysMonSetup($monitor_type = 3, $dir_monitor_path = "C:\", $shell_monitor_path = "")
_FileSysMonSetDirMonPath($dir_monitor_path = "C:\")
_FileSysMonSetShellMonPath($dir_monitor_path = "")
The following example is of a GUI that dynamically updates when files, folders and drives change within the file system (outside of the GUI).
The "Drives" list automatically updates when new drives are connected to the computer, and when existing drives are disconnected. The "Directory Listing" automatically updates whenever an external application (ie. Windows Explorer) adds, renames or deletes files and folders in the directory currently displayed.
When the "Directory Listing" is automatically updated with additions or changes, the list is automatically scrolled to that change.
The user may click on an item in the "Drives" list to change the "Directory Listing" to that drive. The user may also double click on a directory (<DIR>) in the "Directory Listing" to navigate to that directory. Double-clicking on "<DIR> .." will navigate back to the previous directory. The user may also press "F2" to rename an item in the "Directory Listing", and press "Delete" to delete/recycle an item in the "Directory Listing".
FileSystemMonitor Explorer example.au3 9.61K 1900 downloads
The following example monitors file system events. Run this script, and then use various applications (ie. Windows Explorer) whilst the script is running, to manipulate files, folders and drives within "C:\". The changes you make should get recorded in the GUI of this example. The user may also change the Path and click "Use Path" to change monitoring to another path, other than "C:\".
FileSystemMonitor example.au3 4.14K 1966 downloads
Latest Version - v0.4 (02/05/10)
FileSystemMonitor.au3 15.25K 2436 downloads
The script for ReadDirectoryChangesW comes from the following brilliant topic:
[NOW WORKING] a (broken) monitor file changes script which uses ReadDirectoryChangeW
The script for SHChangeNotifyRegister comes from the equally brilliant topic:
SHChangeNotifyRegister() Receive notifications of shell changes.
The combination of the two functions allows monitoring of most I/O functions in Windows, including:
- folder and file renames
- folder and file creates
- folder and file deletes
- folder and file updates
- drive additions
- drive removals
Here's an excerpt:
"...The Origin of Events
So now, you know how to receive any of these shell notifications that are floating around, but who is actually generating them? According to the documentation, 'An application should use this function (SHChangeNotify) if it performs an action that may affect the shell'. But that seems to be a bit of wishful thinking. I can't imagine there are many applica tion developers that really give a damn whether the shell is kept informed of their actions...
...The result is that these notifications tend to be a little bit unreliable. The likelyhood of you getting an event for something, may depend on what explorer windows happen to be open at the time. The shell also only has a 10 item event buffer, and may replace some events with a generic SHCNE_UPDATEDIR in case of an overflow. In short: don't depend on these notifications for any mission-critical applications..."
I then switched to ReadDirectoryChangesW, but found that it lacks in features, such as detecting removable drive additions and removals.
The result I implemented was a combination of the two.
I prefer the Shell Change Notify method much more than the ReadDirectoryChangeW method, though because of the issues with it (as described above) I've had to implement both approaches to catch all events.
I like the fact that the Shell Change Notify method is very asynchronous in nature, running in the background as a registered Windows Message, with a seperate AutoIT function being activated whenever an event occurs, unlike ReadDirectoryChangeW's polling nature. I tried to put the monitoring of ReadDirectoryChangeW inside the MY_SHNOTIFY function above, but it didn't work. I'm still unsure why? If anyone can work it out please let me know. According to other forum topics, each call to ReadDirectoryChangeW should pull the next unprocessed event (that has occurred) out of it's queue (if that's the right word for it). So in theory, it shouldn't matter where it's called, or when. However calling it from within MY_SHNOTIFY doesn't work (even after changing the above code to make all variables global, and therefore accessible to MY_SHNOTIFY). Yet cutting and pasting the ReadDirectoryChangeW script into the Main Loop (While 1) does work. Strange.
Edited by seangriffin, 02 May 2010 - 03:16 AM.