导入csv多个文件



简单任务。只是试图循环浏览.csv文件的文件夹并导入它们。我有点困惑,因为这确实适用于其他脚本,但我一定错过了一些非常明显的东西。

$CSViles在文件夹中正确找到3个文件。但是,$csvOutput只包含文件夹中最后一个文件中的数据。剧本的其余部分运行良好。

$CSVFolder = 'D:XXXX2021_MAY'
$OutputFile = 'D:XXXX2021_MAYmerged_MAY.csv'
$CSVFiles = Get-ChildItem -Path $CSVFolder -Filter *.csv
foreach ($csv in $CSVFiles)
{$csvOutput = Import-Csv $csv.FullName

$group = $csvOutput | Select-Object 
'Channel Reference',
'Received Date',
'Currency',
'Payment Method',
'Channel Payment Reference',
'Line Total Excluding Tax',
'Order Tax','Order Total' | Group-Object {$_.'Channel Reference'} 

$group | Where Count -gt 1 | Foreach-Object 
{
$_.Group[0].'Line Total Excluding Tax' = ($_.Group | Measure-Object 'Line Total Excluding Tax' -Sum).Sum
}
$group | Foreach-Object { $_.Group[0] } | Export-Csv $OutputFile -NoType
}

我现在已经修复了这个

$CSVFolder = 'D:XXXX2021_MAY'
$OutputFile = 'D:XXXX2021_MAYmerged_MAY.csv'
$CSVFiles = Get-ChildItem -Path $CSVFolder -Filter *.csv
foreach ($csv in $CSVFiles){
$group = Import-Csv $csv.FullName | Select-Object 'Channel Reference','Received Date','Currency','Payment Method','Channel Payment Reference','Line Total Excluding Tax','Order Tax','Order Total' | Group-Object {$_.'Channel Reference'} |
Foreach-Object {
if ($_.Count -gt 1){
$_.Group[0].'Line Total Excluding Tax' = ($_.Group | Measure-Object 'Line Total Excluding Tax' -Sum).Sum
}
$_.Group[0]
}
$group | Export-Csv $OutputFile  -notype -append
}

相关内容

  • 没有找到相关文章

最新更新