从目录导入多个CSV文件的更有效方法

时间:2015-06-03 17:41:29

标签: powershell csv

必须有一种更有效的方法将多个CSV文件从文件名包含Services_Results * .csv的目录导入到具有唯一条目的一个变量中。我正在考虑使用通配符循环遍历目录中与文件名匹配的所有文件,然后只导入成功在该字段上的行。

$Success0 = Import-Csv -Path "\\FILE05\Users\USER001\+Projects\Chrome\Services_Results.csv" | Where-Object {$_.Status -eq "Success"}
$Success1 = Import-Csv -Path "\\FILE05\Users\USER001\+Projects\Chrome\Services_Results_150601.csv" | Where-Object {$_.Status -eq "Success"}
$Success2 = Import-Csv -Path "\\FILE05\Users\USER001\+Projects\Chrome\Services_Results_150602.csv" | Where-Object {$_.Status -eq "Success"} 
$Success3 = Import-Csv -Path "\\FILE05\Users\USER001\+Projects\Chrome\Services_Results_150602_b.csv" | Where-Object {$_.Status -eq "Success"} 
$Success4 = Import-Csv -Path "\\FILE05\Users\USER001\+Projects\Chrome\Services_Results_150602_c.csv" | Where-Object {$_.Status -eq "Success"}
$Success5 = Import-Csv -Path "\\FILE05\Users\USER001\+Projects\Chrome\Services_Results_150603_a.csv" | Where-Object {$_.Status -eq "Success"}

$PCList = $Success0 + $Success1 + $Success2 + $Success3 + $Success4 + $Success5
$PCList = $PCList.PC | sort -Unique
Write-host "PCList" $PCList.count

3 个答案:

答案 0 :(得分:2)

来自Get-Help Import-CSV:

-Path <String[]>
    Specifies the path to the CSV file to import. You can also pipe a path to Import-Csv.

    Required?                    false
    Position?                    1
    Default value                None
    Accept pipeline input?       true (ByValue)
    Accept wildcard characters?  false

因此Import-CSV将从管道接受-Path的多个值:

$PCList = 
Get-ChildItem '\\FILE05\Users\USER001\+Projects\Chrome\Services_Results*.csv' |
Select -ExpandProperty FullName |
Import-CSV |
Where-Object {$_.Status -eq "Success"}

答案 1 :(得分:0)

如何使用PowerShell后台作业加快进程?我没有测试过这段代码,但是试着去理解一下这个模式。如果您要导入大型CSV文件,我想您会明白它可以加快这个过程的速度。

### Create an empty array to hold Background Jobs.
$JobList = @();

### Get a list of CSV files.
$FileList = Get-ChildItem -Path \\FILE05\Users\USER001\+Projects\Chrome\*Services_Results*.csv;

foreach ($File in $FileList) {
    ### Start a new Background Job for each file
    $JobList += Start-Job -ScriptBlock { Import-Csv -Path $args[0] | Where-Object -FilterScript { $PSItem.Status -eq 'Success'; } } -ArgumentList $File.FullName -Name $File.Name;
}

### Wait for all the jobs to complete.
Wait-Job -Job $JobList;

### Receive the results of the jobs.
$ResultList = Receive-Job -Job $JobList;

答案 2 :(得分:0)

我认为你可以完全按照你已经做过的方式做你想要的事情,但是使用通配符和ForEach循环:

$files = Get-ChildItem "\\FILE05\Users\USER001\+Projects\Chrome\*.csv"
$PCList = $files | ForEach { (Import-Csv $_ | Where Status -eq "Success") }

$PCList = $PCList | sort -Unique -Property PC
Write-Output $PCList
Write-Output $PCList.Count