【问题标题】:Powershell - Pass list of directory paths to FOR Loop - Output results to CSVPowershell - 将目录路径列表传递给 FOR 循环 - 将结果输出到 CSV
【发布时间】:2017-04-20 23:10:25
【问题描述】:

下面的代码有效。我不想手动指定路径,我想从 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
}

谢谢, 菲尔

【问题讨论】:

    标签: powershell csv powershell-4.0


    【解决方案1】:

    Get-Help Import-Csv 会在这方面为您提供帮助。

    问候,

    kvprasoon

    【讨论】:

      【解决方案2】:

      我假设您想要以下内容:

      # 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"
      }
      

      请注意,您对子树中的子目录进行广度优先枚举的方法是可行的,但对于大型子树来说会很慢。

      【讨论】:

        猜你喜欢
        • 2016-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 1970-01-01
        相关资源
        最近更新 更多