如何在PowerShell 3.0中获取目录深度?

时间:2013-03-03 05:16:02

标签: powershell powershell-v3.0

我需要找出工作目录中的目录结构有多远。如果布局类似于

Books\
Email\
Notes\
    Note 1.txt
    Note 2.txt
HW.docx

然后它应该返回1,因为最深的项目在1级以下。但如果它看起来像

Books\
Photos\
Hello.c

然后它应该返回0,因为没有比第一级更深的东西。

2 个答案:

答案 0 :(得分:1)

这样的事情应该在V3中起作用:

Get-ChildItem . -Recurse -Name | Foreach {($_.ToCharArray() | 
    Where {$_ -eq '\'} | Measure).Count} | Measure -Maximum | Foreach Maximum

答案 1 :(得分:1)

它不是那么漂亮,可以说不像Keith那样“豪华”,但我怀疑它可能会更好地扩展。

$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)