The example scripts below show a clean way of monitoring a folder for changes and responding to those changes.
The _WinAPI_ReadDirectoryChanges()
function provided by AutoIt is quite helpful for this, but unfortunately it is a "blocking" synchronous function, which means that once it is called, it hangs the script and users cannot exit it via the tray icon (well not until a file change is triggered). To workaround this, the scripts below run as a "parent" script and a "child" script. The parent runs the child in a separate process and if the parent is exited, it also exits the child process, which is clean and better for usability.
There's also some helpful comments included in the script, as well as an option of filtering file changes based on a specific file extension, if that's what you need.
Comments and feedback welcome.
Unfortunately the _WinAPI_ReadDirectoryChanges()
function often returns the same filename multiple times when a change is made. If you're moving the file to a new destination, any subsequent operations will fail as the file is no longer present. You will need to handle this situation yourself (unless someone can offer a graceful handling of this).