![](/img/trans.png)
[英]Using PowerShell script, Ping a list of host names and output the results to a csv
[英]Powershell - Pass list of directory paths to FOR Loop - Output results to CSV
下面的代碼有效。 與其手動指定路徑,我不希望從csv文件E:\\ Data \\ paths.csv中傳遞值列表,然后為每個處理的路徑輸出單獨的csv文件,並顯示該目錄的$ Depth ......
$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
}
謝謝,菲爾
Get-Help Import-Csv
將在這方面為您提供幫助。
問候,
kvprasoon
我假設您想要以下內容:
# 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"
}
請注意,您對子樹中子目錄的廣度優先枚舉的方法有效,但是對於大型子樹,它的速度將非常慢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.