[英]Unable to combine all csv files using powershell
I would like to combine all the csv files in my local folder but it shows empty results. 我想合并本地文件夹中的所有csv文件,但显示为空结果。 I am trying to take the header of the first file and skip all the headers in the rest of the files in the folder and join them.
我正在尝试获取第一个文件的标头,并跳过文件夹中其余文件中的所有标头并加入它们。
get-childItem "C:\Users\*.csv" | foreach {[System.IO.File]::AppendAllText
("C:\Users\finalCSV.csv", [System.IO.File]::ReadAllText($_.FullName))}
$getFirstLine = $true
get-childItem "C:\Users\*.csv" | foreach {
$filePath = $_
$lines = $lines = Get-Content $filePath
$linesToWrite = switch($getFirstLine) {
$true {$lines}
$false {$lines | Select -Skip 1}
}
$getFirstLine = $false
Add-Content "C:\Users\finalCSV.csv" $linesToWrite
}
My end result is that when I open finalCSV.csv
it shows no results. 我的最终结果是,当我打开
finalCSV.csv
它没有显示任何结果。
I think you are trying to overwork your solution. 我认为您正在努力解决您的问题。 Just use Import-Csv and append to an array.
只需使用Import-Csv并追加到数组即可。 Something like this:
像这样:
$a = @(); ls *.csv | % {$a += (Import-Csv $_.FullName)}; $a
Works even if the columns are in a different order. 即使列的顺序不同也可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.