【发布时间】:2017-10-17 11:49:11
【问题描述】:
我有这个运行良好的脚本
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\folder_to_scan\"
$watcher.Filter = "*.nrrd"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$action =
{
$path = $Event.SourceEventArgs.FullPath
Write-Host "The file '$path' was $changeType at '$(Get-Date)'" -fore green
}
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 3600}
但是在我的C:\folder_to_scan\ 中,我有很多子目录,例如00123、00245、56002 ... 在每个子目录中我都有\THE_DIRECTORY_TO_SCAN
所以我尝试了这个$watcher.Path = "C:\folder_to_scan\*\THE_DIRECTORY_TO_SCAN\" 和$watcher.Path = "C:\folder_to_scan\[0-9]*\THE_DIRECTORY_TO_SCAN\"
但这不起作用。在这种情况下可以使用通配符吗?
如果不是,如何使用FileSystemWatcher 的多路径?
因为我发现我们可以使用这个
Resolve-Path "C:\folder_to_scan\*\THE_DIRECTORY_TO_SCAN\" | Select -ExpandProperty Path
【问题讨论】:
-
我很抱歉,但它工作得怎么样? while ($true) {sleep 3600} 这只会让你的脚本等待永恒,因为 $true 将永远为真。您想要做的是 do{}while() 那么您想要做的是扫描每个名称如 "THE_DIRECTORY_TO_SCAN" 的子目录?我要做的是使用 Get-childitem 并在文件夹名称中搜索所有具有该名称的路径。当你得到这些,然后你开始专注于监视它们。
-
sleep 3600每小时只监控一次。我缩短了这段代码,因为它更复杂,而且我在action中做了很多事情。无论如何,如何给出FileSystemWatcher的路径列表?
标签: powershell wildcard