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

时间:2017-06-26 13:03:48

标签: powershell recursion powershell-v4.0

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

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

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:\>

2 个答案:

答案 0 :(得分:3)

您无法在PowerShell v4或更早版本中限制128的递归深度。相应参数为added with PowerShell v5

  

<强> -depth

     

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

     

类型:UInt32
  参数集:(全部)
  别名:

     

必填:假   职位:命名为   默认值:无
  接受管道输入:False
  接受通配符:False

您的尝试无效,因为Get-ChildItem -Recurse完全 5级获取文件夹内容。它不包含该级别以上的内容。

如果您无法升级到PowerShell v5,则可以实现一个递归函数,该函数在没有-Path 'H:\demo\*\*\*\*\*'的情况下调用Get-ChildItem。像这样:

-Recurse

答案 1 :(得分:-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