Powershell: 按目录深度获取目录子项
Get-ChildItemToDepth函数通过$Path参数和$ToDepth参数,递归获取$Path下的$ToDepth层目录路径,并输出到STDOUT,用户可以通过管道重定向到其他函数进行二次利用。
函数原型:
Get-ChildItemToDepth $Path $ToDepth
函数定义:
Function Get-ChildItemToDepth {
Param(
[String]$Path = $PWD,
[Byte]$ToDepth = 3,
[Byte]$CurrentDepth = 0
)
$CurrentDepth++
Get-ChildItem -ErrorAction SilentlyContinue $Path | %{
$_
If ($_.PsIsContainer) {
If ($CurrentDepth -le $ToDepth) {
# Callback to this function
Get-ChildItemToDepth -Path $_.FullName `
-ToDepth $ToDepth -CurrentDepth $CurrentDepth
}
} # end if
}
}
函数调用:
Get-ChildItemToDepth "C:\" 3
版权声明
本文出自 Lesca 技术宅,转载时请注明出处及相应链接。
本文永久链接: https://lesca.me/archives/get-directory-entries-by-depth.html

