Note: Running PowerShell v3
I've got a directory setup that is currently:
\ftproot\001\converted
\ftproot\001\inbound
\ftproot\001\pdf
\ftproot\002\converted
\ftproot\002\inbound
\ftproot\002\pdf
\ftproot\xxx\converted
\ftproot\xxx\inbound
\ftproot\xxx\pdf
Each FTP user is mapped to an inbound directory. The structure can be changed if this makes the solution easier
\ftproot\converted\001
\ftproot\converted\002
\ftproot\converted\xxx
\ftproot\inbound\001
\ftproot\inbound\002
\ftproot\inbound\xxx
\ftproot\pdf\001
\ftproot\pdf\002
\ftproot\pdf\xxx
I need to monitor each inbound directory for TIFF files and pickup new FTP users and inbound directories as they are added (following the same structure), so I'm not looking to modify the script for each new user.
The script is currently like:
$fileDirectory = "\ftproot";
$folderMatchString = "^[0-9]{3}$";
$inboundDirectory = "inbound";
$filter = "*.tif";
$directories = dir -Directory $fileDirectory | Where-Object {$_.Name -match $folderMatchString}
foreach ($directory in $directories) {
$sourcePath = "$fileDirectory\$directory\$inboundDirectory"
if(Test-Path -Path $sourcePath -pathType container ) {
// Problem is here //
$fsw = New-Object IO.FileSystemWatcher $sourcePath, $filter -Property @{
IncludeSubdirectories = $false
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
}
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp"
}
I'm wrapping the New-Object IO.FileSystemWatcher in a loop so in the case of the above example, only the last directory will be monitored.
Is it possible to create an array or list or similar of IO.FileSystemWatcher? And if so, how can I code the Register-ObjectEvent for each instance? There will be over a hundred directories to monitor in the end (and slow increasing) but the same action applies to all inbound folders.
Or is there a better solution I should investigate?
The process is the same for each FTP user, the files need to say within directories that can be linked back to the user. The inbound directory will be monitored for uploaded TIFF files, when a file is detected a multi page TIFF is split into individual files and moved into the converted directory, when a new file is detected in converted another action is performed on the TIFF file, then it is converted to PDF and moved to the PDF folder.
Thank you
Solution
$result = @($directoriesOfInterest | ? { Test-Path -Path $_ } | % {
$dir = $_;
$fsw = New-Object IO.FileSystemWatcher $dir, $filter -Property @{
IncludeSubdirectories = $false
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
};
$oc = Register-ObjectEvent $fsw Created -Action {
$path = $Event.SourceEventArgs.FullPath;
$name = $Event.SourceEventArgs.Name;
$changeType = $Event.SourceEventArgs.ChangeType;
$timeStamp = $Event.TimeGenerated;
Write-Host "The file '$name' was $changeType at $timeStamp";
};
new-object PSObject -Property @{ Watcher = $fsw; OnCreated = $oc };
});