vxs8122
vxs8122

Reputation: 844

Merging CSV files

I am trying to merge a number of CSV files into a large CSV file. I wrote a powershell script that successfully create individual csv files for each tag names. But when added Get-Content at the end, I get this error:

Get-Content : An object at the specified path C:\HTD2CSV\Output_Files\*.CSV does not exist, or has been filtered by the
 -Include or -Exclude parameter.
At C:\HTD2CSV\extract.ps1:30 char:20
+ $temp = Get-Content <<<<  "$destinationPath\*.CSV" #| Set-Content $destinationPath\merged.CSV
    + CategoryInfo          : ObjectNotFound: (System.String[]:String[]) [Get-Content], Exception
    + FullyQualifiedErrorId : ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand

But I already have CSV files in the Output_Files folder. Entering Get-Content .\Output_Files\*.CSV worked fine on the command line, but apparently not in the script. My code looks like this:

$currentPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$sourcePath = "C:\Program Files (x86)\Proficy\Proficy iFIX\HTRDATA"
$destinationPath = "$currentPath\Output_Files"

    @(
        "PPP_VE0963A",
        "PPP_VE0963B",
        "PPP_VE0964A",
        "PPP_VE0964B",
        "PPP_VE0967A",
        "PPP_VE0967B",
        "PPP_ZE0963A",
        "PPP_ZE0963B",
        "PPP_ZE0964A",
        "PPP_ZE0964B"
    ) | ForEach-Object {
        .\HTD2CSV.exe `
            PPP:$_.F_CV `
            /dur:00:23:59:00 `
            /int:00:01:00 `
            /sd:05/01/14 `
            /st:00:00:00 `
            /sp:$sourcePath `
            /dp:$destinationPath\$_.CSV `
            /dtf:0 `
            /dbg:0
    }

Get-Content "$destinationPath\*.CSV" | Set-Content "$destinationPath\merged.CSV"

Upvotes: 1

Views: 666

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Don't use Get-Content/Set-Content for merging CSVs (assuming that you actually do have CSVs and not just peculiarly named flat text files). Use Import-Csv and Export-Csv:

Get-ChildItem '*.csv' | % {
  Import-Csv $_.FullName | Export-Csv 'C:\path\to\merged.csv' -NoType -Append
}

or like this (to avoid appending):

Get-ChildItem '*.csv' | % { Import-Csv $_.FullName } |
  Export-Csv 'C:\path\to\merged.csv' -NoType

Upvotes: 2

Related Questions