2016-12-06 30 views
0

下面的代碼有效。我不想手動指定路徑,而想傳遞csv文件E:\ Data \ paths.csv中的值列表,然後爲處理的每個路徑輸出單獨的csv文件,以顯示該目錄的$深度......Powershell - 將目錄路徑列表傳遞給FOR Loop - 將結果輸出到CSV

$StartLevel = 0 # 0 = include base folder, 1 = sub-folders only, 2 = start at 2nd level 
$Depth = 10  # How many levels deep to scan 
$Path = "E:\Data\MyPath"  # starting path 

For ($i=$StartLevel; $i -le $Depth; $i++) { 
$Levels = "\*" * $i 
(Resolve-Path $Path$Levels).ProviderPath | Get-Item | Where PsIsContainer | 
Select FullName 
} 

感謝, 菲爾

回答

0

Get-Help Import-Csv將幫助您在這方面。

問候,

kvprasoon

0

我假設你想要的東西類似如下:

# Create sample input CSV 
@" 
Path,StartLevel,Depth 
"E:\Data\MyPath",0,10 
"@ > PathSpecs.csv 

# Loop over each input CSV row (object with properties 
# .Path, .StartLevel, and .Depth) 
foreach ($pathSpec in Import-Csv PathSpecs.csv) {  
    & { For ([int] $i=$pathSpec.StartLevel; $i -le $pathSpec.Depth; $i++) { 
     $Levels = "\*" * $i 
     Resolve-Path "$($pathSpec.Path)$Levels" | Get-Item | Where PsIsContainer | 
      Select FullName 
    } } | # Export paths to a CSV file named "Path-<input-path-with-punct-stripped>.csv" 
    Export-Csv -NoTypeInformation "Path-$($pathSpec.Path -replace '[^\p{L}0-9]+', '_').csv" 
} 

請注意,您的做法廣度優先枚舉子樹作品子目錄,但會大的子樹很慢。