Limit Get-ChildItem recursion depth

后端 未结 6 1228
清酒与你
清酒与你 2020-11-30 01:21

I can get all sub-items recursively using this command:

Get-ChildItem -recurse

But is there a way to limit the depth? If I only want to rec

6条回答
  •  悲哀的现实
    2020-11-30 01:35

    This is a function that outputs one line per item, with indentation according to depth level. It is probably much more readable.

    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
                }
            }
        }
    }
    

    It is based on a blog post, Practical PowerShell: Pruning File Trees and Extending Cmdlets.

提交回复
热议问题