Note that there are some explanatory texts on larger screens.

plurals
  1. POFilewatcher - catching multiple events issue
    primarykey
    data
    text
    <p>I have a script that watches for files and triggers events on things like change. It works except there are duplicate events triggered for the same file (previous Stackflow question) which I need to handle. I am writing the filename to a logfile and only re-logging it if it is not in there. </p> <p>The problem is I think the events are being fired synchronously and I am getting 2 entries in the log for 1 file change even though I am checking for dups. I am not sure how to work around this.</p> <p>Here is the code:</p> <pre><code>Unregister-Event FW* $source = 'S:\FIS-BIC Reporting\Report Output Files\IT\' $filter = '*.*' $MasterFile = "H:\PS\EmailNotifications.csv" $LogFile = "H:\EmailNotify.log" $FilesToCheck = import-csv -path $Masterfile if (! (Test-Path $Logfile)) { $Output = "Date|Filename|FilenameSpec|Email" $Output | Out-File $LogFile } function Get-CharRight($str,$chars) { return $str.Substring($str.Length – $chars, $chars) } function ProcessFile() { param ([string]$filename) Write-Host "file: $filename" $ext= (Get-CharRight $filename 3) if ($ext -ne "tmp") { foreach($FileToCheck in $FilesToCheck) { if($filename -like $FileToCheck.FilenameSpec) { Write-Host "Match" #check log to see if an email was already sent $Exists = import-csv -path $Logfile -delimiter "|" | where-object {$_.Filename -eq $filename} Write-Host ($Exists.Count -eq $null) if ($Exists.Count -eq $null) { Write-Host "Not in logfile" $Date = Get-Date -format G $Output = $Date + "|" + $filename + "|" + $FileToCheck.FilenameSpec + "|" + $FileToCheck.Email $Output | Out-File $LogFile -append } else {write-host "duplicate"} } } } } $fsw = New-Object IO.FileSystemWatcher $source, $filter -Property @{IncludeSubdirectories = $true; NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite, Attributes'} Register-ObjectEvent $fsw Changed -SourceIdentifier FWFileChanged -Action { ProcessFile $Event.SourceEventArgs.FullPath } Register-ObjectEvent $fsw Created -SourceIdentifier FWFileCreated -Action { ProcessFile $Event.SourceEventArgs.FullPath } while($true) { start-sleep -s 2 } </code></pre> <p>`</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload