简体   繁体   English

扫描Powershell中每个文件夹的特定子文件夹

[英]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\\我有很多子目录像001230024556002 ...,其中的每个人我都\\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.

相关问题 使用Powershell将文件夹结构中的文件复制到其各自的子文件夹 - Copy files in a folder structure to their respective sub-folder with Powershell 在PowerShell中新子文件夹中检测CSV文件 - Detecting csv files in newly sub-folder in PowerShell 创建子文件夹结构 - Creating a sub-folder structure 具有最新写访问权限的输出子文件夹 - Output sub-folder with the latest write access 在每个文件夹中创建一个子文件夹然后将所有文件和文件夹移动到该子文件夹中的代码是什么? - What is a code to create a sub-folder in each folder and then move all the files and folders into that sub-folder? 如何使用powershell从文件夹的根目录中仅删除子文件夹和子文件夹内容而不是单个文件 - How to delete only the sub-folders and sub-folder contents but not individual files from the root of a folder using powershell 使用 powershell 将子文件夹内容上移一级; 不同的父文件夹 - Using powershell to move sub-folder contents up one level; different parent folders 将子文件夹名称导出为路径,并为不同位置的每个父子文件夹创建 csv - Export sub-folder name as path and create a csv for each parent sub-folder in a different location 将文件从子文件夹复制到父文件夹并使用父文件夹名称重命名文件 - Copy file from sub-folder to parent folder and rename the file with parent folder name PowerShell删除最后一个子文件夹 - PowerShell remove last sub folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM