[英]Scan specific sub-folder of every folder in powershell
I have this script which is working fine 我有这个脚本工作正常
$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}
But in my C:\\folder_to_scan\\
I have a lot of sub-directories like 00123
, 00245
, 56002
... And inside each of them I have \\THE_DIRECTORY_TO_SCAN
但在我的C:\\folder_to_scan\\
我有很多子目录像00123
, 00245
, 56002
...,其中的每个人我都\\THE_DIRECTORY_TO_SCAN
So I tried this $watcher.Path = "C:\\folder_to_scan\\*\\THE_DIRECTORY_TO_SCAN\\"
and $watcher.Path = "C:\\folder_to_scan\\[0-9]*\\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\\"
But this is not working. 但这是行不通的。 Is it possible to use wildcard in this situation? 在这种情况下可以使用通配符吗?
If not, how to use multi path with FileSystemWatcher
? 如果没有,如何在FileSystemWatcher
使用多路径? Because I figured out that we can use this 因为我发现我们可以使用它
Resolve-Path "C:\folder_to_scan\*\THE_DIRECTORY_TO_SCAN\" | Select -ExpandProperty Path
I'm not familiar with FileSystemWatcher
so I am assuming you just need to feed in the folder paths to $watcher.Path
我不熟悉FileSystemWatcher
所以我假设您只需要将文件夹路径输入$watcher.Path
I'm also assuming your code works already. 我还假设您的代码已经可以使用。
try using Get-ChildItem
and use a for loop: 尝试使用Get-ChildItem
并使用for循环:
$watcher = New-Object System.IO.FileSystemWatcher
Get-ChildItem "C:\folder_to_scan\" -Recurse | ? { $_.PSIsContainer -and $_.Name.EndsWith("THE_DIRECTORY_TO_SCAN")}| %{
$watcher.Path = $_.FullName #change this
$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}
Ok so to clarify what i was talking about earlier. 好吧,以澄清我刚才在说什么。 You are only repeating your wait-function. 您只是在重复您的等待功能。 To show you what i mean : 告诉你我的意思:
echo "This would be your code running."
while ($true) {sleep -second 3}
This would print the sentence once and then keep waiting for eternity for 3 seconds each time $true is true. 这将打印该语句一次,然后每次$ true为true时都等待永恒3秒钟。
What you need to do is : 您需要做的是:
do{
echo "This would be your code running."
sleep -seconds 3
} while($true)
This will print the sentence every 3 seconds. 这将每3秒打印一次该句子。 Or in your case, run your watcher every 3 seconds. 或者,就您而言,每3秒运行一次观察者。
I didn't find a perfect solution so I did this 我没有找到完美的解决方案,所以我做到了
if ( $path -like '*\THE_DIRECTORY_TO_SCAN\*' )
{ Write-Host "File created in \THE_DIRECTORY_TO_SCAN" -fore green }
else
{ Write-Host "File created NOT in \THE_DIRECTORY_TO_SCAN" -fore red }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.