列出Powershell中给定深度以下的文件夹 [英] List folders at or below a given depth in Powershell

查看:127
本文介绍了列出Powershell中给定深度以下的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多文件夹的目录。我想列出深度超过2级的所有文件夹(路径)。因此,在以下情况下,文件夹1& 2。

I have a directory which contains a lot of folders. I want to list all folder (path) that go deeper than 2 levels. So in below case folder 1 & 2.

Directory/folder1
Directory/folder1/test1/test/testsub
Directory/folder1/test2
Directory/folder1/test3
Directory/folder2/blablabla/bla/1
Directory/folder3/test
Directory/folder4/test
Directory/folder5/test

我正在尝试以下操作:

$Depth = 3
$Path = "."

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


推荐答案

下面紧接的解决方案您自己假设您的意图是找到那些子树超过给定深度的子目录

The solution immediately below, which builds on your own, assumes that your intent is to find those child directories whose subtrees exceed a given depth.

如果您想查找位于给定深度或更深的所有目录路径,请参见底部。

您的方法无法实现,因为它只能在给定深度找到目录,而不是在下面。

If you instead want to find all directory paths that are at a given depth or deeper, see the bottom section.
Your approach cannot achieve that, because it finds directories at the given depth only, not also below.

您自己的目录聪明的基于通配符的方法原则上应该可行,但是:

Your own clever wildcard-based approach should work in principle, but:


  • (a)可以大大简化。

  • (a) it can be greatly streamlined.

(b)需要做更多工作才能将输出限制到子树过深的子文件夹的不同列表。

(b) additional work is needed to limit output to the distinct list of those child folders whose subtrees are too deep.

$Depth = 3
$Path = '.'

$Levels = '/*' * $Depth
Get-ChildItem -Directory $Path/$Levels




  • 与您自己的方法一样,'/ *'* $ Depth 动态创建多目录级通配符表达式(例如, $ Depth / * / * c $ c> of 2 )t可以将hat附加到输入 $ Path 上,以仅匹配该级别的路径。

    • As in your own approach, '/*' * $Depth dynamically creates a multi-directory-level wildcard expression (e.g., /*/* for a $Depth of 2) that can be appended to the input $Path to match only paths at that level.

      -Directory 开关(PSv3 +)仅限制与目录匹配。

      The -Directory switch (PSv3+) limits matching to directories only.

      $Depth = 3
      $Path = '.'
      
      $Levels = '/*' * $Depth
      Get-ChildItem -Directory $Path/$Levels |
        ForEach-Object { ($_.FullName -split '[\\/]')[-$Depth] } |
          Select-Object -Unique
      

      注意:按 [/ \\] -即通过 / \ -使解决方案也可以在类似Unix的平台上运行(PowerShell Core );在Windows上,用分割'\\'(由转义的 \ )是足够的。

      Note: Splitting by [/\\] - that is, by either / or \ - makes the solution work on Unix-like platforms too (PowerShell Core); on Windows, -split '\\' (by an escaped \) is sufficient.

      使用示例文件夹层次结构,以上内容将产生:

      With your sample folder hierarchy, the above would yield:

      folder1
      folder2
      




      • 如果需要完整路径,而是附加
        | Convert-Path -LiteralPath { $ Path / $ _}

        如果要目录信息对象 [System.IO.DirectoryInfo] ),而是附加
        | Get-Item -LiteralPath { $ Path / $ _}

        If you want directory-info objects ([System.IO.DirectoryInfo]) instead, append
        | Get-Item -LiteralPath { "$Path/$_" }.

        注意:


        • 即使目标文件夹(目录)下的解决方案也可以通过简单地包含文件省略-目录或仅通过将-目录替换为-文件

        • Even though the solutions below target folders (directories), you can include files too by simply omitting -Directory, or target files only by replacing -Directory with -File.

        为简单起见,这些命令隐式地针对当前目录。

        For simplicity, the commands implicitly target the current directory.

        按给定深度 逻辑:

        At-a-given-depth-only logic:

        这与上面的解决方案中使用的逻辑相同;下面的代码列出了深度仅为2的文件夹,即子级(即子目录的子目录)-注意,与 Get-ChildItem -Depth ,深度计数以 1 开头,即 1 指向子目录:

        This is the same logic employed in the solution above; the following code lists folders at depth 2 only, i.e. those at the grandchild level (child directories of child directories) - note that, unlike with Get-ChildItem -Depth, depth counting starts with 1, i.e. 1 refers to child directories:

        $depth = 2 # grandchild directories only
        
        Get-ChildItem -Directory -Path ('*/' * $depth)
        




        • 输出完整路径 ,将 Get-ChildItem 命令放在(...)中。全名或将其通过管道传输到 Select-Object -ExpandProperty FullName

          • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.

            输出相对路径(例如, folder1 / test1 / test / testsub ),还需要其他工作,因为添加-名称在这种情况下可以正常工作(它只会输出目录名称):

            To output relative paths (e.g., folder1/test1/test/testsub), additional work is needed, because adding -Name will not work as expected in this case (it will output just the directory names):

            $depth = 2 # grandchild directories
            
            # Calculate the length of the path prefix for determining relative paths.
            # (Using the current dir ($PWD) as the reference path here.)
            $PrefixLen = (Convert-Path -LiteralPath $PWD).Length + 1
            
            $Levels = '/*' * $Depth
            Get-ChildItem -Directory -Path ('*/' * $depth) |
              ForEach-Object { $_.FullName.Substring($PrefixLen) }
            

            达到-给定的深度逻辑:

            Up-to-a-given-depth logic:

            PSv5 + -深度 参数限制 Get-ChildItem 的递归深度,即,它只能找到不超过指定的深度,但请注意,代表深度的是深度 0 ,而不是 1 直接孩子

            请注意,使用-深度 暗含 -递归,尽管您也可以指定后者。

            The PSv5+ -Depth parameter limits Get-ChildItem's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth 0, not 1 that represents the immediate children.
            Note that use of -Depth implies -Recurse, though you may specify the latter as well.

            例如,枚举子文件夹和孙子文件夹(2个级别)当前目录,使用:

            For instance, to enumerate child folders and grandchild folders (2 levels) in the current directory, use:

            $depth = 2 # child and grandchild directories
            
            Get-ChildItem -Directory -Depth ($depth - 1)
            




            • 要输出完整路径,将 Get-ChildItem 命令括在(。 。)。全名或将其通过管道传输到 Select-Object -ExpandProperty FullName

              • To output full paths, enclose the Get-ChildItem command in (...).FullName or pipe it to Select-Object -ExpandProperty FullName.

                要输出相对路径,只需将-名称开关添加到 Get-ChildItem 致电。

                To output relative paths, simply add the -Name switch to the Get-ChildItem call.

                给定深度或更深 em>逻辑:

                At-a-given-depth-or-deeper logic:

                将结果限制为大于或等于给定深度的项目需要自定义解决方案:

                Limiting results to items at levels greater than or equal to a given depth requires a custom solution:

                $depth = 2 # grandchild directories and below
                
                Get-ChildItem -Directory -Name -Recurse |
                  Where-Object { ($_ -split '[/\\]').Count -ge 2 } |
                    Get-Item -LiteralPath { "$PWD/$_" }
                

                如果您的输入路径不是(隐含的)当前目录,请将该路径替换为 $ PWD


                • 要输出完整路径,请将 Get-Item 替换为转换路径

                要输出相对路径,只需省略 Get-Item 调用。

                To output relative paths, simply omit the Get-Item call.

                这篇关于列出Powershell中给定深度以下的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆