2013-03-03 54 views
1

我需要找出工作目錄內的目錄結構向下多遠。如果佈局是一樣的東西如何在PowerShell 3.0中獲得目錄深度?

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

那麼它應該返回1,因爲最深的項目如下1水平。但是,如果它看起來像

Books\ 
Photos\ 
Hello.c 

那麼它應該返回0,因爲沒有什麼比第一層次更深。

回答

1

像這樣的東西應該做的伎倆在V3:

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

這不漂亮,可以說是不作爲「辣妹」基思的,但我懷疑它會變得更好。

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