Get-ChildItem 재귀 수준 제한
이 명령을 사용하여 모든 하위 항목을 재귀 적으로 가져올 수 있습니다.
Get-ChildItem -recurse
그러나 깊이를 제한하는 방법이 있습니까? 예를 들어 한두 단계 만 되풀이하려면?
당신은 시도 할 수 있습니다:
Get-ChildItem \*\*\*
이렇게하면 깊이가 두 개의 하위 폴더 인 모든 항목이 반환됩니다. \ *를 추가하면 검색 할 추가 하위 폴더가 추가됩니다.
get-childitem을 사용하여 재귀 검색을 제한하는 OP 질문과 함께 검색 할 수있는 모든 깊이를 지정해야합니다.
Get-ChildItem \*\*\*,\*\*,\*
각 깊이 2,1 및 0에서 자식을 반환합니다.
powershell 5.0 부터 이제 ! 에서 -Depth
매개 변수를 사용할 수 있습니다 Get-ChildItem
.
-Recurse
재귀를 제한하기 위해 결합합니다 .
Get-ChildItem -Recurse -Depth 2
이 기능을 시도하십시오 :
Function Get-ChildItemToDepth {
Param(
[String]$Path = $PWD,
[String]$Filter = "*",
[Byte]$ToDepth = 255,
[Byte]$CurrentDepth = 0,
[Switch]$DebugMode
)
$CurrentDepth++
If ($DebugMode) {
$DebugPreference = "Continue"
}
Get-ChildItem $Path | %{
$_ | ?{ $_.Name -Like $Filter }
If ($_.PsIsContainer) {
If ($CurrentDepth -le $ToDepth) {
# Callback to this function
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter `
-ToDepth $ToDepth -CurrentDepth $CurrentDepth
}
Else {
Write-Debug $("Skipping GCI for Folder: $($_.FullName) " + `
"(Why: Current depth $CurrentDepth vs limit depth $ToDepth)")
}
}
}
}
Resolve-Path를 사용하여 Get-ChildItem 재귀 깊이를 제한하려고했습니다.
$PATH = "."
$folder = get-item $PATH
$FolderFullName = $Folder.FullName
$PATHs = Resolve-Path $FolderFullName\*\*\*\
$Folders = $PATHs | get-item | where {$_.PsIsContainer}
그러나 이것은 잘 작동합니다.
gci "$PATH\*\*\*\*"
This is a function that outputs one line per item, with indentation according to depth level. It is probably much more readable.
function GetDirs($path = $pwd, [Byte]$ToDepth = 255, [Byte]$CurrentDepth = 0)
{
$CurrentDepth++
If ($CurrentDepth -le $ToDepth) {
foreach ($item in Get-ChildItem $path)
{
if (Test-Path $item.FullName -PathType Container)
{
"." * $CurrentDepth + $item.FullName
GetDirs $item.FullName -ToDepth $ToDepth -CurrentDepth $CurrentDepth
}
}
}
}
It is based on a blog post, Practical PowerShell: Pruning File Trees and Extending Cmdlets.
@scanlegentil I like this.
A little improvement would be:
$Depth = 2
$Path = "."
$Levels = "\*" * $Depth
$Folder = Get-Item $Path
$FolderFullName = $Folder.FullName
Resolve-Path $FolderFullName$Levels | Get-Item | ? {$_.PsIsContainer} | Write-Host
As mentioned, this would only scan the specified depth, so this modification is an improvement:
$StartLevel = 1 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level
$Depth = 2 # How many levels deep to scan
$Path = "." # starting path
For ($i=$StartLevel; $i -le $Depth; $i++) {
$Levels = "\*" * $i
(Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer |
Select FullName
}
참고URL : https://stackoverflow.com/questions/13249085/limit-get-childitem-recursion-depth
'code' 카테고리의 다른 글
MVC 패턴과 스윙 (0) | 2020.10.26 |
---|---|
Express에서 루트 후 선택적 매개 변수로 경로 제어를 전달합니까? (0) | 2020.10.26 |
드롭 다운 방법 (0) | 2020.10.26 |
뒤로 버튼과 같은 버튼을 사용하여 현재 조각을 닫는 방법은 무엇입니까? (0) | 2020.10.25 |
셔플 된 연속 정수 배열에서 중복 요소를 찾는 방법은 무엇입니까? (0) | 2020.10.25 |