Search code examples
powershellfilterfile-watcher

Filter to not include multiple extensions


Recently I have connected my FTP to Sharefile. Sharfile creates a .syncdb file in my ftp folders. I have code that checks my FTP folder for new files created, copies them to a new folder and sends notification emails that files have arrived. Sometimes I am now receiving emails for .syncdb-wal and syncdb-shm files. This actually doesn't create the file to be copied, but it does create a notification email and creates a blank folder which is a nuisance. I am trying to just not include these files with a filter but it doesn't seem to be working. I am not sure if you can declare more than one extension for the filter. Here is the code below that I am trying to use to filter files to not include files with the extensions .syncdb-wal and syncdb-shm, I am probably missing something easy.

$MonitorFolder = Get-Content "C:\Users\RickG\Desktop\ScanFTPDeptClients\Pathlist.txt"

$filter ='*.syncdb-wal, *.syncdb-shm '
foreach ($path in $MonitorFolder){
$watcher = New-Object System.IO.FileSystemWatcher $path, -ne $filter
#Files only. Default is files + directory
$watcher.NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite'
}

Solution

  • No you can't apply multiple file name filters.

    Inspect the name of the file in the event handler instead

    $watcher = New-Object System.IO.FileSystemWatcher $path
    $watcher.NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite'
    $watcher.Filter = '*.*'
    
    Register-ObjectEvent $watcher -EventName Created -Action {
        if ($EventArgs.Name -eq '.syncdb-wal' -or $EventArgs.Name -eq '.syncdb-shm'){
            # nope, not interested
            return
        }
    }