如何在不添加引号的情况下将列添加到单个目录中的多个 CSV 文件中?
How to add columns into multiple CSV files in a single directory without adding quotation marks?
我在一个目录中有数千个 CSV 文件。我正在寻找一种方法如何向每个文件添加 2 列(包括 headers)。
有一些条件:
第 5 列始终为 0
在第 6 列中,我想存储不带扩展名 (ABC) 的文件名
输入文件示例(文件名是 ABC.CSV)
HEADER1,HEADER2,HEADER3,HEADER4
04/22/2012,47.64,47.97,47.05
04/23/2012,47.6,48.2,47.4
04/24/2012,48.13,48.33,47.84
04/25/2012,47.81,48.14,47.59
04/26/2012,47.83,48.21,47.49
04/27/2012,47.2,47.31,46.84
04/28/2012,47.01,47.05,46.33
我在下面发布的代码有 1 个问题,
- 它为新文件中的每个值添加引号 ("04/22/2012","47.64","47.97","47.05","0","ABC")。
我需要的输出文件示例
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5,HEADER6
04/22/2012,47.64,47.97,47.05,0,ABC
04/23/2012,47.6,48.2,47.4,0,ABC
04/24/2012,48.13,48.33,47.84,0,ABC
04/25/2012,47.81,48.14,47.59,0,ABC
04/26/2012,47.83,48.21,47.49,0,ABC
04/27/2012,47.2,47.31,46.84,0,ABC
04/28/2012,47.01,47.05,46.33,0,ABC
$files = Get-ChildItem ".\" -filter "*.csv"
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
$csv = Import-Csv $files[$i].FullName
$newcsv = @()
foreach ( $row in $csv ) {
$row | Add-Member -MemberType NoteProperty -Name 'HEADER 5' -Value '0'
$row | Add-Member -MemberType NoteProperty -Name 'HEADER 6' -Value $files[$i].BaseName
$newcsv += $row
}
$newcsv | Export-Csv $files[$i].FullName -NoTypeInformation
}
还有一个问题。因为我在一个目录中有数千个文件,所以这段代码是否足够高效以尽可能快地完成任务?
有人已经建议我改进代码,而不是遍历行,而是考虑使用 select
构建 te 成员
$csv = $csv | select-object *,@{n="HEADER5";e={0}},@{n="HEADER6";e={$file.BaseName}}
但我不知道如何将他的建议应用到我的代码中。
我认为我原来的答案过于依赖 v3/v4 东西,下面的东西怎么样:
$files = Get-ChildItem ".\" | Where-Object { $_.Extension -eq ".csv" }
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
$csv = Import-Csv $files[$i].FullName
$newcsv = @()
foreach ( $row in $csv ) {
$row | Add-Member -MemberType NoteProperty -Name 'HEADER5' -Value '0'
$row | Add-Member -MemberType NoteProperty -Name 'HEADER6' -Value $files[$i].BaseName
$newcsv += $row
}
( $newcsv | ConvertTo-Csv -NoTypeInformation ) | Foreach-Object { $_ -replace '"', '' } | Out-File $outfile
}
未测试:
$InputFolder = 'c:\SomeFolder'
$OutputFolder = 'c:\SomeOtherFolder'
Get-ChildItem $InputFolder -Filter *.* |
where {-not $_.psiscontainer} |
foreach {
$FileName = $_.Name
$BaseName = $_.Basename
$data = Get-Content $_ -ReadCount 0
"$($data[0]),Header5,Header6" | Set-Content $OutputFolder$FileName
$data[1..($data.Length -1)] -replace '$',",0,$BaseName" |
Add-Content $OutputFolder$FileName
}
我在一个目录中有数千个 CSV 文件。我正在寻找一种方法如何向每个文件添加 2 列(包括 headers)。
有一些条件:
第 5 列始终为 0
在第 6 列中,我想存储不带扩展名 (ABC) 的文件名
输入文件示例(文件名是 ABC.CSV)
HEADER1,HEADER2,HEADER3,HEADER4
04/22/2012,47.64,47.97,47.05
04/23/2012,47.6,48.2,47.4
04/24/2012,48.13,48.33,47.84
04/25/2012,47.81,48.14,47.59
04/26/2012,47.83,48.21,47.49
04/27/2012,47.2,47.31,46.84
04/28/2012,47.01,47.05,46.33
我在下面发布的代码有 1 个问题,
- 它为新文件中的每个值添加引号 ("04/22/2012","47.64","47.97","47.05","0","ABC")。
我需要的输出文件示例
HEADER1,HEADER2,HEADER3,HEADER4,HEADER5,HEADER6
04/22/2012,47.64,47.97,47.05,0,ABC
04/23/2012,47.6,48.2,47.4,0,ABC
04/24/2012,48.13,48.33,47.84,0,ABC
04/25/2012,47.81,48.14,47.59,0,ABC
04/26/2012,47.83,48.21,47.49,0,ABC
04/27/2012,47.2,47.31,46.84,0,ABC
04/28/2012,47.01,47.05,46.33,0,ABC
$files = Get-ChildItem ".\" -filter "*.csv"
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
$csv = Import-Csv $files[$i].FullName
$newcsv = @()
foreach ( $row in $csv ) {
$row | Add-Member -MemberType NoteProperty -Name 'HEADER 5' -Value '0'
$row | Add-Member -MemberType NoteProperty -Name 'HEADER 6' -Value $files[$i].BaseName
$newcsv += $row
}
$newcsv | Export-Csv $files[$i].FullName -NoTypeInformation
}
还有一个问题。因为我在一个目录中有数千个文件,所以这段代码是否足够高效以尽可能快地完成任务?
有人已经建议我改进代码,而不是遍历行,而是考虑使用 select
构建 te 成员$csv = $csv | select-object *,@{n="HEADER5";e={0}},@{n="HEADER6";e={$file.BaseName}}
但我不知道如何将他的建议应用到我的代码中。
我认为我原来的答案过于依赖 v3/v4 东西,下面的东西怎么样:
$files = Get-ChildItem ".\" | Where-Object { $_.Extension -eq ".csv" }
for ($i=0; $i -lt $files.Count; $i++) {
$outfile = $files[$i].FullName + "out"
$csv = Import-Csv $files[$i].FullName
$newcsv = @()
foreach ( $row in $csv ) {
$row | Add-Member -MemberType NoteProperty -Name 'HEADER5' -Value '0'
$row | Add-Member -MemberType NoteProperty -Name 'HEADER6' -Value $files[$i].BaseName
$newcsv += $row
}
( $newcsv | ConvertTo-Csv -NoTypeInformation ) | Foreach-Object { $_ -replace '"', '' } | Out-File $outfile
}
未测试:
$InputFolder = 'c:\SomeFolder'
$OutputFolder = 'c:\SomeOtherFolder'
Get-ChildItem $InputFolder -Filter *.* |
where {-not $_.psiscontainer} |
foreach {
$FileName = $_.Name
$BaseName = $_.Basename
$data = Get-Content $_ -ReadCount 0
"$($data[0]),Header5,Header6" | Set-Content $OutputFolder$FileName
$data[1..($data.Length -1)] -replace '$',",0,$BaseName" |
Add-Content $OutputFolder$FileName
}