반응형
Powershell 의 Get-ChildItem 을 사용하여 파일 또는 폴더의 리스트를 얻을 때
해당 폴더 하위의 끝까지 검색을 하고자 할때는 –recurse 라는 옵션을 사용합니다.
그런데 특정 depth 까지만 검색을 하고자 하는 경우 \* 와 같은 키워드를 사용하기도 하지만
Get-ChildItemToDepth 라는 함수가 아래 주소에 있어서 소개하고자 합니다.
http://www.indented.co.uk/2010/01/22/limit-recursion-depth-with-get-childitem/
코드는 다음과 같습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ForEach-Object { | |
$_ | Where-Object { $_.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)") | |
} | |
} | |
} | |
} |
특정 폴더 이하의 D:\ 의 2 depth 까지 검색하는 방법은 다음과 같습니다.
get-childitemtodepth -Path "D:\*" -ToDepth 2
반응형
'programming language > powershell' 카테고리의 다른 글
Powershell 에서 Outlook 으로 메일 보내기 (0) | 2016.03.06 |
---|---|
Powershell 를 활용한 Network Drive 찾기 (0) | 2016.02.16 |
Powershell 매월 X 째 주 Y요일 찾기 (0) | 2016.01.19 |
Powershell 에서 exe 실행 시 끝날 때 까지 기다리게 하기 (0) | 2016.01.04 |
Powershell 을 이용한 소스코드 라인 수 세기 (0) | 2015.05.19 |
Powershell 에서 7z명령어 사용하기 (0) | 2015.05.05 |
효율적인 Powershell 코딩을 위한 Powershell IDE, Powershell Plus (0) | 2014.04.10 |
Powershell 현재 스크립트 파일의 디렉터리 (0) | 2014.01.20 |
댓글