简体   繁体   中英

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......

$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.

regards,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM