I have created a IO.filesystemwatcher
to monitor a folder and take an action when certain file types are written to the location. I am looking for file types .jpg
and .tmp
. I have named the filter as a variable, and the filter works when including one file type, but not two types.
Code below functions correctly:
$filter = '*.jpg'
New-Object IO.FileSystemWatcher $Folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Code below functions correctly:
$filter = '*.tmp'
New-Object IO.FileSystemWatcher $Folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
Code below DOES NOT function:
$filter = '*.tmp','*jpg'
New-Object IO.FileSystemWatcher $Folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}
I have also tried $filter = '*.tmp' -or '*jpg'
I am sure there's a different way to do this to make it work, but I am not very good at working with IO.filesystemwatcher
. Any recommendations are appreciated.
Thanks
The .Filter
property is [string]
-typed and supports only a single wildcard expressions; from the docs:
Use of multiple filters such as
"*.txt|*.doc"
is not supported.
It sounds like you'll have to:
either: watch for changes to all files, by setting .Filter
to ''
(the empty string), and then perform your own filtering inside your event handler.
or: set up a separate watcher instance for each filter (wildcard pattern). Thanks, mhhollomon.