搜索文件夹时限制递归深度

问题描述 投票:0回答:2

我试图限制我写的文件夹搜索脚本的递归深度。我试图限制到最多五个级别

基本上我想得到这样的东西:

h:\demo\1st level
h:\demo\1st level\2nd level
h:\demo\1st level\2nd level\3rd level
h:\demo\1st level\2nd level\3rd level\4th level\
h:\demo\1st level\2nd level\3rd level\4th level\5th level

这是我的代码:

function Get-ChildItemRecursive {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true, ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]

        [string]$FullName,
        [Parameter(Mandatory=$false)]

        [int]$Depth = 0
    )

    Process {
        if ($Depth -ne 1) {
            $newDepth = if ($Depth -gt 1) { $Depth - 1 } else { $Depth }
            Get-ChildItem -Path $FullName -Directory |
                Get-ChildItemRecursive -Depth $newDepth
        }
        else {
            Get-ChildItem -Path $FullName -Directory
        }
        Get-ChildItem -Path $FullName -File
    }
}

Get-ChildItemRecursive -FullName 'H:\demo\' |
   Where {$_.PSIsContainer -eq $True} | select @{Name='Date Modified';
Expression = {$_.LastWriteTime.ToString('MM/dd/yyyy')}},
             @{Name='Owner';E=
                 {(($_.GetAccessControl().Owner.Split('\'))[1])}},
             FullName | Export-Csv 'H:\demo\scan1.csv' -NoTypeInformation

我得到的输出:

Get-ChildItemRecursive -FullName 'H:\demo\' |
    Where {$_.PSIsContainer -eq $True} | select   @{Name='Date Modified';
Expression = {$_.LastWriteTime.ToString('MM/dd/yyyy')}},
             @{Name='Owner';E=
                 {(($_.GetAccessControl().Owner.Split('\'))[1])}},
             FullName | Export-Csv 'H:\demo\scan1.csv' -NoTypeInformation

PS H:\> Get-ChildItemRecursive
cmdlet Get-ChildItemRecursive at command pipeline position 1
Supply values for the following parameters:
FullName: H:\demo\

Directory: H:\demo

Mode                LastWriteTime     Length Name

----                -------------     ------ ----

-a---         6/21/2017   4:12 PM     248472 lastrun.csv

-a---         6/26/2017  11:27 AM        706 demo1.csv

-a---         6/21/2017   1:38 PM       7861 4thrun06-21-17.csv

-a---         6/21/2017  11:50 AM       2182 firstrun06-21-17.csv

-a---         6/21/2017   2:41 PM       1334 demo.csv

-a---         6/21/2017  12:24 PM      20985 3rdrun06-21-17.csv

-a---         6/26/2017   2:24 PM          0 scan1.csv

-a---         6/21/2017   4:22 PM       3671 sort-parent-subfolder.csv

-a---         6/21/2017  12:25 PM       7298 2ndrun06-21-17.csv

-a---         6/22/2017   4:46 PM       4637 2ndfolderRun6-22-17.csv

-a---         6/22/2017  10:59 AM      28540 firstfolder.csv

-a---         6/22/2017   4:59 PM     104618 4thfolder.csv


PS H:\>
powershell recursion powershell-v4.0
2个回答
3
投票

您无法在PowerShell v4或更早版本中限制Get-ChildItem -Recurse的递归深度。相应的参数是added with PowerShell v5

-深度

在Powershell 5.0中添加的此参数使您可以控制递归的深度。您可以使用-Recurse和-Depth参数来限制递归。

输入:UInt32 参数集:(全部) 别名:

必需:错误 职位:命名 默认值:无 接受管道输入:False 接受通配符:False

并且你的尝试不起作用,因为-Path 'H:\demo\*\*\*\*\*'从5级深度获取文件夹内容。它不包括该级别以上的内容。

如果无法升级到PowerShell v5,则可以实现一个递归函数,该函数在没有Get-ChildItem的情况下调用-Recurse。像这样的东西:

function Get-ChildItemRecursive {
  [CmdletBinding()]
  Param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [string]$FullName,
    [Parameter(Mandatory=$false)]
    [int]$Depth = 0
  )

  Process {
    if ($Depth -ne 1) {
      $newDepth = if ($Depth -gt 1) { $Depth - 1 } else { $Depth }
      Get-ChildItem -Path $FullName -Directory | Get-ChildItemRecursive -Depth $newDepth
    }
    Get-ChildItem -Path $FullName
  }
}

Get-ChildItemRecursive -FullName 'H:\demo' -Depth 5 |
    Where {$_.PSIsContainer -eq $True} |
    ...

-3
投票

它不是那么美丽,但你可以试试这个:

# First lvl
Get-ChildItem -Recurse -Path 'D:\Test' | Where {$_.PSIsContainer -eq $True} | select  @{Name='Date Modified'; Expression={$_.LastWriteTime.ToString('MM/dd/yyyy')}},  @{Name='Owner';E={(($_.GetAccessControl().Owner.Split('\'))[1])}}, @{Name='FullnameLvl0';E={(($_.Fullname.Split('\')[0] + "\" + $_.Fullname.Split('\')[1]))}} | add-content D:\Test\test.csv

# Sec lvl
Get-ChildItem -Recurse -Path 'D:\Test' | Where {$_.PSIsContainer -eq $True} | select  @{Name='Date Modified'; Expression={$_.LastWriteTime.ToString('MM/dd/yyyy')}},  @{Name='Owner';E={(($_.GetAccessControl().Owner.Split('\'))[1])}}, @{Name='FullnameLvl1';E={(($_.Fullname.Split('\')[0] + "\" + $_.Fullname.Split('\')[1])+ "\" + $_.Fullname.Split('\')[2])}} | add-content D:\Test\test.csv

#...etc
© www.soinside.com 2019 - 2024. All rights reserved.