john50
john50

Reputation: 467

How to join multiple files in two directories in powershell?

I have 2 folders OldData and NewData. In both folders I have a thousands of csv files with the same names. Looking for a most effective way how to merge files with the same names from both folders and store them in NewData folder.

Example:

OldData folder:
test1.csv
test2.csv
.
.

NewData folder:

test1.csv (same names than in OldData folders but not exactly the same content)
test2.csv
.
.

In NewData folder there should be merged files from both folders. Files in OldData folder have header. Files in new data folder are without header, but they have same structure(same number of columns).

Thank you for any help.

Upvotes: 0

Views: 936

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Something like this should work:

Get-ChildItem 'C:\OldData\*.csv' | % {
  $dst = Join-Path 'C:\NewData' $_.Name
  Import-Csv $_.FullName | Export-Csv $dst -Append
}

Edit: If the files in OldData do have headers while the files in NewData don't, I'd append the new files to the old ones, not the other way around.

Get-ChildItem 'C:\NewData\*.csv' | % {
  $dst = Join-Path 'C:\OldData' $_.Name
  if (Test-Path -LiteralPath $dst) {
    Get-Content $_.FullName | Add-Content $dst
  }
}

Upvotes: 1

Related Questions