[英]Powershell - Pass list of directory paths to FOR Loop - Output results to CSV
The code below works. 下面的代码有效。 Rather than specify the path manually I would like to pass a list of values from a csv file E:\\Data\\paths.csv and then output individual csv files for each path processed displaying the $Depth for that directory......
与其手动指定路径,我不希望从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
}
Thanks, Phil 谢谢,菲尔
Get-Help Import-Csv
will help you in this regards. Get-Help Import-Csv
将在这方面为您提供帮助。
regards, 问候,
kvprasoon kvprasoon
I assume you want something like the following: 我假设您想要以下内容:
# 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"
}
Note that your approach to breadth-first enumeration of subdirectories in the subtree works, but will be quite slow with large subtrees. 请注意,您对子树中子目录的广度优先枚举的方法有效,但是对于大型子树,它的速度将非常慢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.