Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PowerShell 3.0中获取目录深度?_Powershell_Powershell 3.0 - Fatal编程技术网

如何在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)