在不覆盖现有数据的情况下更新 .csv 中的数据/在 powershell 中添加新数据

Updating data in .csv without overwriting the existing data / adding new data in powershell

又是我这个 Powershell 新手(我现在使用 powershell 已经 11 天了)。

到目前为止我遇到的问题是什么: 我正在从不同的文件夹中读取 csv 文件并将数据合并到一个大的 csv (bigCSV) 中。 bigCSV 是一个动态文档,因此我手动将数据添加到此工作表中。

What I do / Example

问题是,如果我必须更新 bigCSV(使用新的 csv 文件),现有的 bigCSV 会被覆盖,我手动添加的数据也会丢失。

What I do / The Problem

我尝试在我的代码中添加-append,但结果并不令人满意,因为手动添加的数据仍然存在,但现在我有重复的...

New Problem

到目前为止,这是我的代码...

$Userpath_Access    = "$PSScriptRoot"
$txtFiles = Get-ChildItem -Path $Userpath_Access\txt-Import-Datenbank\ -Filter *.csv -Recurse -ErrorAction SilentlyContinue -Force

$csv = foreach ($file in $txtFiles) {
        Import-Csv -Path $file.FullName -Delimiter ";" | 
                      Select-Object 'Type (BAY)', 'Order Number (BAY)', 'PCP Number (PCP)', @{label='Project ID (Mosaic)';expression={$($_.'Project')}}, 'Priority (BAY)', @{label='Compound No (Mosaic)';expression={$($_.'Compound No')}},
                      @{label='Customer (Mosaic)';expression={$($_.'Requestor')}}, @{label='Date Order Created (Mosaic)';expression={$($_.'Order Date')}},  @{label='Comment (Mosaic)';expression={$($_.'Comment')}}, 'Status', 'Data Finalized', @{label='Compound Amount [mg] (Mosaic)';expression={$($_.'Amount')}},
                      'Mail Address', 'Duration', @{label='Name Of Customer (Mosaic)';expression={$($_.'Requestor')}}, @{label='Vial (Mosaic)';expression={$($_.'Vial')}}, @{label='Mosaic File (Mosaic)';expression={$($_.'Filename')}}, @{label='Date Received (Mosaic)';expression={$($_.'Data import')}},
                      @{label='Order ID (Mosaic)';expression={$($_.'Order ID')}}
}

$fileOut = Join-Path -Path $Userpath_Access -ChildPath ('Inh_Auftragsliste.csv')
$csv | Export-Csv -Path $fileOut -Delimiter ";" -NoTypeInformation -Append

现在我正在寻找解决方案,只需将“新文件”添加到我现有的 bigCSV。

您可以将 CSV 文件导入 Excel 对其进行排序并使用 -Unique 标志,然后再次导出它。

Import-Csv C:\temp\UsersConsolidated.csv | sort lname,fname –Unique | Export-Csv C:\temp\UsersConsolidated.csv

我认为在导出到 CSV 时没有办法处理它。

如果我正确地理解了这个问题,那么你的代码已经不远了。 最主要的是,我认为您需要将 Import-Csv 和 Select-Object 的转换数据添加到大 CSV 文件 inside 循环中。

尝试:

$Userpath_Access = $PSScriptRoot
$fileOut = Join-Path -Path $Userpath_Access -ChildPath 'Inh_Auftragsliste.csv'  # the BigCSV

$txtFiles = Get-ChildItem -Path "$Userpath_Access\txt-Import-Datenbank" -Filter '*.csv' -File -Recurse -ErrorAction SilentlyContinue -Force

foreach ($file in $txtFiles) {
    Write-Host "Adding data from 'file $($file.FullName)'.."

    Import-Csv -Path $file.FullName -Delimiter ";" | 
        Select-Object 'Type (BAY)', 'Order Number (BAY)', 'PCP Number (PCP)', 
                      @{label='Project ID (Mosaic)';expression={$_.'Project'}}, 
                      'Priority (BAY)', 
                      @{label='Compound No (Mosaic)';expression={$_.'Compound No'}},
                      @{label='Customer (Mosaic)';expression={$_.'Requestor'}}, 
                      @{label='Date Order Created (Mosaic)';expression={$_.'Order Date'}},  
                      @{label='Comment (Mosaic)';expression={$_.'Comment'}}, 
                      'Status', 'Data Finalized',
                      @{label='Compound Amount [mg] (Mosaic)';expression={$_.'Amount'}},
                      'Mail Address', 'Duration', 
                      @{label='Name Of Customer (Mosaic)';expression={$_.'Requestor'}},
                      @{label='Vial (Mosaic)';expression={$_.'Vial'}}, 
                      @{label='Mosaic File (Mosaic)';expression={$_.'Filename'}}, 
                      @{label='Date Received (Mosaic)';expression={$_.'Data import'}},
                      @{label='Order ID (Mosaic)';expression={$_.'Order ID'}} |
    Export-Csv -Path $fileOut -Delimiter ";" -NoTypeInformation -Append
}

小细节,但我已经更改了引用和不必要的 $() 一点

查看 ,如果我理解正确,您必须反对以下列表:

$Old = Import-Csv .\Old.csv

Date       Filename  Type (BAY) ...
----       --------  ---------- ---
2020-08-01 File1.csv Type 1     Info 1
2020-08-02 File2.csv Type 2
2020-08-03 File3.csv Type 3

并且:

$New = Import-Csv .\New.csv

Date       Filename  Type (BAY) ...
----       --------  ---------- ---
2020-08-04 File2.csv Type 2     Info 2
2020-08-04 File4.csv Type 4     Info 4

如果你使用这个 Join-Object (see also: In Powershell, what's the best way to join two tables into one?),你可以像这样合并两个列表:

$Old | Merge-Object $New -on Filename

Date       Filename  Type (BAY) ...
----       --------  ---------- ---
2020-08-01 File1.csv Type 1     Info 1
2020-08-04 File2.csv Type 2     Info 2
2020-08-03 File3.csv Type 3
2020-08-04 File4.csv Type 4     Info 4