如何在PowerShell 3.0中获取目录深度?
我需要找出一个工作目录中的目录结构到底有多远。如果布局类似于如何在PowerShell 3.0中获取目录深度?,powershell,powershell-3.0,Powershell,Powershell 3.0,我需要找出一个工作目录中的目录结构到底有多远。如果布局类似于 Books\ Email\ Notes\ Note 1.txt Note 2.txt HW.docx 然后它应该返回1,因为最深的项目是1级别以下。但是如果它看起来像 Books\ Photos\ Hello.c 然后它应该返回0,因为没有比第一层更深的东西了。类似这样的东西应该在V3中起作用: Get-ChildItem . -Recurse -Name | Foreach {($_.ToCharArray()
Books\
Email\
Notes\
Note 1.txt
Note 2.txt
HW.docx
然后它应该返回1
,因为最深的项目是1
级别以下。但是如果它看起来像
Books\
Photos\
Hello.c
然后它应该返回
0
,因为没有比第一层更深的东西了。类似这样的东西应该在V3中起作用:
Get-ChildItem . -Recurse -Name | Foreach {($_.ToCharArray() |
Where {$_ -eq '\'} | Measure).Count} | Measure -Maximum | Foreach Maximum
它不像基思的那样漂亮,也可以说不像基思的那样“时髦”,但我怀疑它的规模可能会更好
$depth_ht = @{}
(cmd /c dir /ad /s) -replace '[^\\]','' |
foreach {$depth_ht[$_]++}
$max_depth =
$depth_ht.keys |
sort length |
select -last 1 |
select -ExpandProperty length
$root_depth =
($PWD -replace '[^\\]','').length
($max_depth -$root_depth)