列出Powershell中给定深度或以下的文件夹
我有一个包含很多文件夹的目录。我想列出所有超过2级的文件夹(路径)。因此,在下面的案例中,文件夹1和2列出Powershell中给定深度或以下的文件夹,powershell,recursion,depth,directory-structure,Powershell,Recursion,Depth,Directory Structure,我有一个包含很多文件夹的目录。我想列出所有超过2级的文件夹(路径)。因此,在下面的案例中,文件夹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/folde
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
请试试这个
$Folders = Get-ChildItem 'C:\Program Files' -Directory -Recurse -Depth 1 |
Select-Object -ExpandProperty fullname |
Sort-Object
$Folders
下面的解决方案建立在您自己的基础上,假设您的目的是查找子树超过给定深度的子目录 如果您想查找给定深度或更深的所有目录路径,请参阅底部部分。
您的方法无法实现这一点,因为它只在给定深度处查找目录,而不在下面
您自己的基于通配符的聪明方法原则上应该有效,但是:
- (a) 它可以大大简化
- (b) 需要进行额外的工作,以将输出限制为子树太深的子文件夹的不同列表
- 与您自己的方法一样,
动态创建一个多目录级别的通配符表达式(例如,'/*$Depth
用于/*/*
的$Depth
),该表达式可以附加到输入$Depth
以仅匹配该级别的路径$Path
开关(PSv3+)限制仅匹配目录-Directory
[/\\]
-也就是说,通过/
或\
进行拆分-使解决方案也能在类似Unix的平台上工作(PowerShell Core);在Windows上,-split'\\'
(通过转义的\
)就足够了
使用示例文件夹层次结构,上述内容将产生:
folder1
folder2
- 如果您想要完整的路径,请附加
|Convert Path-LiteralPath{“$Path/$\”}
- 如果您想要目录信息对象(
),请附加[System.IO.DirectoryInfo]
| Get Item-LiteralPath{“$Path/$”}
可选阅读:获取达到、达到或超过某个深度的文件夹: 注:
- 即使解决方案位于目标文件夹(目录)下,也可以通过简单地省略
来包含文件,或者仅通过将-Directory
替换为-Directory
来包含目标文件-File
- 为简单起见,这些命令隐式地以当前目录为目标
At-a-给定的仅深度逻辑: 这与上述解决方案中采用的逻辑相同;以下代码仅列出深度为2的文件夹,即孙子级别的文件夹(子目录的子目录)-注意,与
Get ChildItem-depth
不同,深度计数从1
开始,即1
指代子目录:
$depth = 2 # grandchild directories only
Get-ChildItem -Directory -Path ('*/' * $depth)
- 要输出完整路径,请将
命令括在Get ChildItem
中,或将其输送到(…).FullName
选择对象-ExpandProperty FullName
- 要输出相对路径(例如,
),需要进行额外的工作,因为在这种情况下,添加folder1/test1/test/testsub
将无法按预期工作(它将只输出目录名):-Name
-Depth
参数限制Get ChildItem
的递归深度,即它只查找指定深度的项,但请注意,表示直接子项的是Depth0
,而不是1
请注意,使用-Depth
意味着-Recurse
,尽管您也可以指定后者
例如,要枚举当前目录中的子文件夹和孙子文件夹(2级),请使用:
$depth = 2 # child and grandchild directories
Get-ChildItem -Directory -Depth ($depth - 1)
- 要输出完整路径,请将
命令括在Get ChildItem
中,或将其输送到(…).FullName
选择对象-ExpandProperty FullName
- 要输出相对路径,只需将
开关添加到-Name
调用getchilditem
$depth = 2 # grandchild directories and below
Get-ChildItem -Directory -Name -Recurse |
Where-Object { ($_ -split '[/\\]').Count -ge 2 } |
Get-Item -LiteralPath { "$PWD/$_" }
如果输入路径不是(隐含的)当前目录,请将该路径替换为$PWD
- 要输出完整路径,请将
替换为Get Item
Convert Path
- 要输出相对路径,只需省略
调用Get Item
-Depth 1
将递归限制在前两个级别,因此您不知道哪些目录的子树更深入,而OP的目标是什么。
$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) }
$depth = 2 # child and grandchild directories
Get-ChildItem -Directory -Depth ($depth - 1)
$depth = 2 # grandchild directories and below
Get-ChildItem -Directory -Name -Recurse |
Where-Object { ($_ -split '[/\\]').Count -ge 2 } |
Get-Item -LiteralPath { "$PWD/$_" }