I'm attempting to monitor my download folder and trigger a command-line action when a download is completed. This is very similar to another question, How to monitor a folder and trigger a command-line action when a file is created or edited?
I've implemented the PowerShell script described in the most upvoted answer, but when I run it, I'm finding that the output is confusing.
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "Z:\UnprocessedDownloads"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date -f o), $changeType, $path"
Add-content "Z:\log.txt" -value $logline
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
Register-ObjectEvent $watcher "Changed" -Action $action
Register-ObjectEvent $watcher "Deleted" -Action $action
Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}
I should note that the downloads in question are being created by Chrome, which may or may not explain what's going on. As best I can tell, the series of events is as follows:
- A file with the intended filename is created.
- The previously created file is promptly deleted.
- A file with the intended filename, and .crdownload appended, is created.
- This file registers between two and three changes (of the files I tested, if it was a few KB it showed two, but files 100MB to 10GB registered three).
- This file is renamed, removing the .crdownload.
- This file then registers an additional two to three changes (I couldn't find any rhyme or reason to how many occurred).
- It's done, nothing else registers without manual intervention.
So, that's that. I'm at a loss as to how I can tell a file is truly "done", short of perhaps watching for a rename event, and subsequently waiting for an arbitrary number of seconds (occasionally, the last recorded change took about 5 seconds to occur - I just realized this is likely due to the sleep 5 in the script), perhaps 10.
Can anyone explain what's going on here? Are there any suggestions or alternatives? I'm not beholden to PowerShell, it just seemed like it and the File System Watcher would be well suited to the task. I'd prefer something with as little overhead as possible while retaining automation.
FWIW, an excerpt from my tests (hardly extensive; note - I did adjust the log to show milliseconds):
2019-01-02T22:03:03.6712039-05:00, Created, Z:\Unprocessed\test (1).jpg
2019-01-02T22:03:03.7242040-05:00, Deleted, Z:\Unprocessed\test (1).jpg
2019-01-02T22:03:03.7252054-05:00, Created, Z:\Unprocessed\test (1).jpg.crdownload
2019-01-02T22:03:03.7252054-05:00, Changed, Z:\Unprocessed\test (1).jpg.crdownload
2019-01-02T22:03:08.7265875-05:00, Changed, Z:\Unprocessed\test (1).jpg.crdownload
2019-01-02T22:03:08.7305994-05:00, Renamed, Z:\Unprocessed\test (1).jpg
2019-01-02T22:03:08.7315887-05:00, Changed, Z:\Unprocessed\test (1).jpg
2019-01-02T22:03:08.7315887-05:00, Changed, Z:\Unprocessed\test (1).jpg
2019-01-02T22:03:13.7348367-05:00, Changed, Z:\Unprocessed\test (1).jpg
2019-01-02T22:09:28.7729475-05:00, Deleted, Z:\Unprocessed\10GB.bin
2019-01-02T22:09:33.7742846-05:00, Created, Z:\Unprocessed\1GB.bin
2019-01-02T22:09:33.7762852-05:00, Deleted, Z:\Unprocessed\1GB.bin
2019-01-02T22:09:33.7772866-05:00, Created, Z:\Unprocessed\1GB.bin.crdownload
2019-01-02T22:09:33.7782853-05:00, Changed, Z:\Unprocessed\1GB.bin.crdownload
2019-01-02T22:09:33.7792825-05:00, Changed, Z:\Unprocessed\1GB.bin.crdownload
2019-01-02T22:10:28.7850646-05:00, Changed, Z:\Unprocessed\1GB.bin.crdownload
2019-01-02T22:10:28.7860648-05:00, Renamed, Z:\Unprocessed\1GB.bin
2019-01-02T22:10:28.7870652-05:00, Changed, Z:\Unprocessed\1GB.bin
2019-01-02T22:10:28.7870652-05:00, Changed, Z:\Unprocessed\1GB.bin
2019-01-02T22:10:28.7880654-05:00, Changed, Z:\Unprocessed\1GB.bin
2019-01-02T22:11:13.7928495-05:00, Created, Z:\Unprocessed\test (2).jpg
2019-01-02T22:11:13.7938482-05:00, Deleted, Z:\Unprocessed\test (2).jpg
2019-01-02T22:11:13.7938482-05:00, Created, Z:\Unprocessed\test (2).jpg.crdownload
2019-01-02T22:11:13.7948490-05:00, Changed, Z:\Unprocessed\test (2).jpg.crdownload
2019-01-02T22:11:18.7972830-05:00, Changed, Z:\Unprocessed\test (2).jpg.crdownload
2019-01-02T22:11:18.7982945-05:00, Renamed, Z:\Unprocessed\test (2).jpg
2019-01-02T22:11:18.7992839-05:00, Changed, Z:\Unprocessed\test (2).jpg
2019-01-02T22:11:18.8002947-05:00, Changed, Z:\Unprocessed\test (2).jpg
2019-01-02T22:11:23.8011169-05:00, Changed, Z:\Unprocessed\test (2).jpg