【问题标题】:Updating data in .csv without overwriting the existing data / adding new data in powershell更新 .csv 中的数据而不覆盖现有数据/在 powershell 中添加新数据
【发布时间】:2020-11-24 06:51:29
【问题描述】:

我又是 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。

【问题讨论】:

  • 我们还没有收到您的来信.. 任何给定的答案是否解决了您的问题?如果是这样,请点击左侧的✓ 考虑accepting。这将帮助其他有类似问题的人更轻松地找到它。
  • 我很抱歉......科罗娜抓住了我,我完全错过了提供一些反馈。 :-(
  • 哇,好重……希望你现在感觉好多了。
  • 是的,我又恢复了健康,我希望,从现在开始我就免疫了。 :)

标签: powershell csv merge updates


【解决方案1】:

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

试试:

$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
}

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      $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(另请参阅: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
      

      【讨论】:

        猜你喜欢
        • 2013-06-29
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 2018-03-06
        相关资源
        最近更新 更多