简体   繁体   English

Powershell-将目录路径列表传递到FOR Loop-将结果输出到CSV

[英]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.

相关问题 使用 PowerShell 脚本,Ping 主机名列表和 output 结果到 csv - Using PowerShell script, Ping a list of host names and output the results to a csv Ping 主机名列表并将结果输出到 powershell 中的 csv - Ping a list of host names and output the results to a csv in powershell 循环浏览目录并使用Powershell删除csv标头 - Loop through directory and remove csv headers with Powershell 从Powershell foreach循环中将输出安排到CSV - Arranging output to CSV from Powershell foreach loop Powershell,为每个循环运行并将输出导出到.csv - Powershell, Run For Each Loop and Export Output to .csv 将可变输出中的多个值循环到csv Powershell - Loop Multiple Values In Variable Output To csv Powershell Powershell 循环通过。CSV 创建分发列表? - Powershell to loop through .CSV to create Distribution List? 简单的PowerShell脚本,要从CSV获取PATHS,请确认这些路径是否存在,并编写另一个具有真实结果的CSV文件 - Simple PowerShell Script, to obtain PATHS from a CSV, confirm that those exist and write another CSV with the true results 如何将源路径列表中定义的文件复制到 cmd 或 powershell 中的 output 路径列表中? - How to copy files defined in a list of source paths to a list of output paths in cmd or powershell? 在Powershell中对路径列表进行排序 - Sorting a list of paths in Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM