Powershell: 按目录深度获取目录子项
作者: lesca
分类: Powershell, Tutorials
发布时间: 2012-10-24 11:23
ė浏览 4,498 次
6Comments Off on Powershell: 按目录深度获取目录子项
Get-ChildItemToDepth函数通过Path参数和ToDepth参数,递归获取Path下的ToDepth层目录路径,并输出到STDOUT,用户可以通过管道重定向到其他函数进行二次利用。
函数原型:
Get-ChildItemToDepth PathToDepth
函数定义:
Function Get-ChildItemToDepth { Param( [String]Path =PWD, [Byte]ToDepth = 3, [Byte]CurrentDepth = 0 ) CurrentDepth++ Get-ChildItem -ErrorAction SilentlyContinuePath | %{ _ If (_.PsIsContainer) { If (CurrentDepth -leToDepth) { # Callback to this function Get-ChildItemToDepth -Path _.FullName ` -ToDepthToDepth -CurrentDepth $CurrentDepth } } # end if } }
函数调用:
Get-ChildItemToDepth "C:\" 3
Copyright
本文出自 Lesca 技术宅,转载时请注明出处及相应链接。
本文永久链接: https://lesca.me/archives/get-directory-entries-by-depth.html