Powershell - 将列添加到 CSV 末尾

问题描述 投票:0回答:2

今天早上我发现了一个 powershell 脚本,它会将创建的日期添加到 CSV 中的每一行。我得到它几乎完全符合我的需要,但我希望添加在每行的末尾,而不是在开头。有人可以帮忙吗?

这个可以用,但是顺序错误

Get-ChildItem . -Recurse -File -Filter *.txt -Exclude merged.csv -PipelineVariable csv |
  ForEach-Object FullName | # parameter binding problem otherwise...
  Import-Csv |
  Select-Object @{n='Timestamp';e={$csv.LastWriteTime.ToString('d')}} , * |
  Export-Csv -NTI merged.csv

我尝试添加 -Indexed 但要么它位于错误的位置,要么我需要实际索引对象?我不明白该怎么做。

不起作用

Get-ChildItem . -Recurse -File -Filter *.txt -Exclude merged.csv -PipelineVariable csv |
  ForEach-Object FullName | # parameter binding problem otherwise...
  Import-Csv |
  Select-Object -Index (n-1), ($a.count - 1) @{n='Timestamp';e={$csv.LastWriteTime.ToString('s')}} , * |
  Export-Csv -NTI merged.csv
windows powershell csv export
2个回答
0
投票

Select-Object
是在开头添加新列,先导入CSV文件,然后添加新列。

Get-ChildItem . -Recurse -File -Filter *.txt -Exclude merged.csv -PipelineVariable csv |
ForEach-Object FullName | # parameter binding problem otherwise...
Import-Csv |
ForEach-Object { Add-Member -InputObject $_ -NotePropertyName 'Timestamp' -NotePropertyValue $csv.LastWriteTime.ToString('d') -PassThru } |
Export-Csv -NTI merged.csv

试试这个,如果有帮助请告诉我。


0
投票

获取子项。 -Recurse -File -Filter *.txt -排除 merged.csv -PipelineVariable csv | ForEach-对象全名 | # 否则参数绑定问题... 导入-CSV | ForEach-Object { Add-Member -InputObject $_ -NotePropertyName 'Timestamp' -NotePropertyValue $csv.LastWriteTime.ToString('d') -PassThru } | 导出-CSV -NTI merged.csv

© www.soinside.com 2019 - 2024. All rights reserved.