列出Powershell中给定深度以下的文件夹 [英] List folders at or below a given depth in 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> of2
)t可以将hat附加到输入$ Path
上,以仅匹配该级别的路径。As in your own approach,
'/*' * $Depth
dynamically creates a multi-directory-level wildcard expression (e.g.,/*/*
for a$Depth
of2
) 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/$_" }
.注意:
-
即使目标文件夹(目录)下的解决方案也可以通过简单地包含文件省略
-目录
或仅通过将-目录
替换为-文件$的目标文件c $ c>。
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 $不同c $ c>,深度计数以
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 with1
, 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 toSelect-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 limitsGet-ChildItem
's recursion depth, i.e., it only finds items up to the specified depth, but note that it is depth0
, not1
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 toSelect-Object -ExpandProperty FullName
.要输出相对路径,只需将
-名称
开关添加到Get-ChildItem
致电。To output relative paths, simply add the
-Name
switch to theGet-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屋!
-
-
-
-