Powershell监视多个目录 [英] Powershell monitor multiple directories
问题描述
注意:运行PowerShell v3
Note: Running PowerShell v3
我的目录设置当前为:
\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
每个FTP用户都映射到一个入站目录.如果可以简化解决方案,则可以更改结构
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
我需要监视每个入站目录中的TIFF文件,并在添加新FTP用户和入站目录时采用它们(遵循相同的结构),因此我不希望为每个新用户修改脚本.
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.
该脚本当前类似于:
$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"
}
我将新对象IO.FileSystemWatcher循环包装,因此在上面的示例中,将仅监视最后一个目录.
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.
是否可以创建IO.FileSystemWatcher的数组或列表或类似内容?如果是这样,我该如何为每个实例编码Register-ObjectEvent?最终将有一百多个目录要监视(并且缓慢增加),但是相同的操作适用于所有入站文件夹.
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?
每个FTP用户的过程相同,文件必须在可以链接回该用户的目录中说.将监视入站目录中是否有上载的TIFF文件,当检测到文件时,将多页TIFF拆分为单个文件并移到转换后的目录中,当在转换中检测到新文件时,会对TIFF文件执行另一操作,然后它将转换为PDF并移至PDF文件夹.
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.
谢谢
解决方案
$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 };
});
推荐答案
是否可以创建IO.FileSystemWatcher的数组或列表或类似文件?
Is it possible to create an array or list or similar of IO.FileSystemWatcher?
是的.利用管道就像:
$fsws = $directoriesOfInterest | ? { Test-Path -Path $_ } | % {
new-object IO.FileSystemWatcher …
}
如果创建了多个观察者,则
和$fsws
将是一个数组(如果只有一个观察者,则将是一个浪费,如果没有,则将是$null
).要始终获取数组,请在@(…)
中放置管道:
and $fsws
will be an array, if there is more than one watcher created (it will be a waster if there is only one, $null
if there are none). To always get an array put the pipeline in @(…)
:
$fsws = @($directoriesOfInterest | ? { Test-Path -Path $_ } | % {
new-object IO.FileSystemWatcher …
})
如果是这样,如何为每个实例编码Register-ObjectEvent?
if so, how can I code the Register-ObjectEvent for each instance?
将对象注册放在同一循环中,并返回观察者和事件注册:
Put the object registration in the same loop, and return both the watcher and the event registration:
$result = @($directoriesOfInterest | ? { Test-Path -Path $_ } | % {
# $_ may have a different value in code in inner pipelines, so give it a
# different name.
$dir = $_;
$fsw = new-object IO.FileSystemWatcher $dir, …;
$oc = Register-ObjectEvent $fsw Created …;
new-object PSObject -props @{ Watcher = $fsw; OnCreated = $oc };
})
和$result
将是这两个属性的对象数组.
and $result
will be an array of objects will those two properties.
请注意,传递给Register-ObjectEvent
的-Action
代码可以引用$fsw
和$dir
,从而知道哪个观察者触发了事件.
Note the -Action
code passed to Register-ObjectEvent
can reference $fsw
and $dir
and thus know which watcher has fired its event.
这篇关于Powershell监视多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!