grizlyadams
grizlyadams

Reputation: 41

Usinf System.IO.FileSystemWatcher to write to a log file when a file is created

I'm trying to monitor a directory for the creation of files. If a file is created, I want to write a single line out to a logfile and carry on monitoring the folder. I'm using this code below but I get a double entry in the log file.

If I put breaks into the $action I can see that it runs through that twice before waiting for the next event, which is why it writes two line to the log file.

What have I got wrong?

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\test-folder"
$watcher.Filter = "*FINAL*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true 
$watcher.InternalBufferSize = 16384 

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Add-Content "C:\test-folder\created.log" -value $logline
          }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
$created = Register-ObjectEvent $watcher "Created" -Action $action
#    $changed = Register-ObjectEvent $watcher "Changed" -Action $action
#    $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
#    $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 5}

log file looks like this...

05/03/2016 11:25:16, Created, C:\test-folder\test1-FINAL-.txt
05/03/2016 11:25:16, Created, C:\test-folder\test1-FINAL-.txt
05/03/2016 11:26:10, Created, C:\test-folder\test2-FINAL-.txt
05/03/2016 11:26:10, Created, C:\test-folder\test2-FINAL-.txt

Upvotes: 2

Views: 1108

Answers (2)

grizlyadams
grizlyadams

Reputation: 41

I ended up using the timestamp to set a global variable that I compare against to check if its a duplicate.

not the best solution as potentially anything created a second apart will still result in a double entry, but it works in our situation.

thanks for everyone's input.

Upvotes: 1

Edijs Perkums
Edijs Perkums

Reputation: 422

You should read this topic LastWrite FileSystemWatcher Powershell: notification

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.

Upvotes: 2

Related Questions