【问题标题】:Limit Get-ChildItem recursion depth限制 Get-ChildItem 递归深度
【发布时间】:2012-10-26 06:59:01
【问题描述】:

我可以使用这个命令递归获取所有子项:

Get-ChildItem -recurse

但是有没有办法限制深度?例如,如果我只想向下递归一两个级别?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    使用它来将深度限制为 2:

    Get-ChildItem \*\*\*,\*\*,\*
    

    它的工作方式是返回每个深度为 2,1 和 0 的孩子。


    解释:

    这个命令

    Get-ChildItem \*\*\*
    

    返回深度为两个子文件夹的所有项目。添加 \* 会添加一个额外的子文件夹进行搜索。

    根据 OP 问题,要使用 get-childitem 限制递归搜索,您需要指定可以搜索的所有深度。

    【讨论】:

    • 太棒了。我想我从来不需要在 PowerShell 中执行此操作,但我知道我之前在 CMD.EXE 中希望这样做。
    • 似乎你必须做 "-path \*\*\*,\*\*,\*" 来拉入中间深度。
    • 我试过了,但它似乎上升而不是下降。如何使用它生成文件夹树,如 superuser.com/questions/258287/… ?谢谢。
    • 要从当前目录开始递归,需要在每条路径的开头添加一个点。 Get-ChildItem .\*\*\*,.\*\*,.\* 在某些时候,通过 PowerShell 5.1,Get-ChildItem 现在有一个 -Depth 参数。
    【解决方案2】:

    试试这个功能:

    Function Get-ChildItemToDepth {
        Param(
            [String]$Path = $PWD,
            [String]$Filter = "*",
            [Byte]$ToDepth = 255,
            [Byte]$CurrentDepth = 0,
            [Switch]$DebugMode
        )
    
        $CurrentDepth++
        If ($DebugMode) {
            $DebugPreference = "Continue"
        }
    
        Get-ChildItem $Path | %{
            $_ | ?{ $_.Name -Like $Filter }
    
            If ($_.PsIsContainer) {
                If ($CurrentDepth -le $ToDepth) {
    
                    # Callback to this function
                    Get-ChildItemToDepth -Path $_.FullName -Filter $Filter `
                      -ToDepth $ToDepth -CurrentDepth $CurrentDepth
                }
                Else {
                    Write-Debug $("Skipping GCI for Folder: $($_.FullName) " + `
                      "(Why: Current depth $CurrentDepth vs limit depth $ToDepth)")
                }
            }
        }
    }
    

    source

    【讨论】:

    • 它确实有效。示例调用:Get-ChildItemToDepth -ToDepth 2(在当前目录中工作)
    【解决方案3】:

    我尝试使用 Resolve-Path 限制 Get-ChildItem 递归深度

    $PATH = "."
    $folder = get-item $PATH 
    $FolderFullName = $Folder.FullName
    $PATHs = Resolve-Path $FolderFullName\*\*\*\
    $Folders = $PATHs | get-item | where {$_.PsIsContainer}
    

    但这很好用:

    gci "$PATH\*\*\*\*"
    

    【讨论】:

      【解决方案4】:

      @scanlegentil 我喜欢这个。
      一点改进将是:

      $Depth = 2
      $Path = "."
      
      $Levels = "\*" * $Depth
      $Folder = Get-Item $Path
      $FolderFullName = $Folder.FullName
      Resolve-Path $FolderFullName$Levels | Get-Item | ? {$_.PsIsContainer} | Write-Host
      

      如前所述,这只会扫描指定的深度,所以这个修改是一个改进:

      $StartLevel = 1 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
      $Depth = 2      # How many levels deep to scan
      $Path = "."     # starting path
      
      For ($i=$StartLevel; $i -le $Depth; $i++) {
          $Levels = "\*" * $i
          (Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
          Select FullName
      }
      

      【讨论】:

      • 这将显示指定深度的所有子文件夹名称
      • 这似乎显示了在给定深度的项目,而不是到给定深度。
      【解决方案5】:

      这是一个函数,每个项目输出一行,根据深度级别缩进。它可能更具可读性。

      function GetDirs($path = $pwd, [Byte]$ToDepth = 255, [Byte]$CurrentDepth = 0)
      {
          $CurrentDepth++
          If ($CurrentDepth -le $ToDepth) {
              foreach ($item in Get-ChildItem $path)
              {
                  if (Test-Path $item.FullName -PathType Container)
                  {
                      "." * $CurrentDepth + $item.FullName
                      GetDirs $item.FullName -ToDepth $ToDepth -CurrentDepth $CurrentDepth
                  }
              }
          }
      }
      

      它基于一篇博文,Practical PowerShell: Pruning File Trees and Extending Cmdlets。

      【讨论】:

        【解决方案6】:

        As of powershell 5.0,您现在可以在Get-ChildItem 中使用-Depth 参数!

        你将它与-Recurse 结合起来以限制递归。

        Get-ChildItem -Recurse -Depth 2
        

        【讨论】:

        • 在指定-Depth 时,-Recurse 开关似乎是可选的/隐含的。
        • 我发现这似乎很奇怪的一件事是 -Exclude 当包含在 -Depth 中时会否定 -Depth 值。
        • @ssaviers -Include 也会出现同样的问题。
        • 对于它的价值, -Depth 参数在与通配符结合时似乎被忽略了。例如,gci c:\*.exe -Depth 1 返回多个子目录的文件。
        • 很高兴知道! gci c:\ -filter *.exe -depth 1 可能会得到你想要的东西 @GuitarPicker 我现在没有要测试的 Windows 机器
        【解决方案7】:

        $path = C:
        $depth = 0 #, 0 是基数,1 个文件夹更深,2 个文件夹更深

        Get-ChildItem -Path $path -Depth $depth | Where-Object {$_.Extension -eq ".extension"}

        【讨论】:

        • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-02
        • 2011-02-07
        • 2020-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多