1

N number of CSV file to combine into one file. and the data must reside side by side, Something like this

DatTime_M1  Voltage Currency| Dattime_m2 Volage currency |.....DatTime_N Vol  Curr
2016-04-29  237.1   3.54    2016-04-29  237.1   3.54   2016-04-29   237.1   3.54
2016-04-29  237.7   3.54   2016-04-29   237.1   3.54   2016-04-29   237.1   3.54
2016-04-29  236.4   3.54    2016-04-29  237.1   3.54   2016-04-29   237.1   3.54
2016-04-29  236.4   3.53    2016-04-29  237.1   3.54   2016-04-29   237.1   3.54

I have written a code where instead of above merge in simply merges or appends in the same row.

string[] files = (Directory.GetFiles(txtConsolidated_OS_CSV.Text.Trim()));
                    foreach (var file in files)
                    {
                        StringBuilder sb = new StringBuilder();
                        string filename = Path.GetFileNameWithoutExtension(file);
                        if (file.EndsWith(".csv"))
                        {
                            string[] rows = File.ReadAllLines(file);
                            for (int i = 0; i < rows.Length; i++)
                            {
                                if (i == 0)
                                {
                                    if (counter == 0)
                                    {
                                        sb.Append(rows[i] + "\n");
                                        counter++;
                                    }
                                }
                                else
                                {
                                    sb.Append(rows[i] + "\n");
                                }
                            }
                        }
                        string csvfile = txtConsolidated_OS_CSV.Text + "\\Merged_OS.csv";

                        if (File.Exists(csvfile))
                        {
                            File.AppendAllText(csvfile, sb.ToString());
                            sb.Clear();
                        }
                        else
                        {
                            File.WriteAllText(csvfile, sb.ToString());
                            sb.Clear();
                        }
anil gadiyar
  • 65
  • 1
  • 9
  • Ok, as a human how would you do it? looking at your screen its unclear how your 3 files are supposed to arrive in a combined file, do you need contents 1 followed by contents 2, then 3.. one after the other, or, are you trying to get fields from each of the files and make one wide file with all 3 bits of information tied together> – BugFinder Apr 29 '16 at 10:08

1 Answers1

1

With LINQ:

var allCsv = Directory.EnumerateFiles("Src-Path", "*.csv", SearchOption.TopDirectoryOnly);
string[] header = 
{ 
    File.ReadLines(allCsv.First()).First(l => !string.IsNullOrWhiteSpace(l)) 
};

 // Get CSV Data
 var mergedData = allCsv.SelectMany(csv => File.ReadLines(csv).SkipWhile(l => string.IsNullOrWhiteSpace(l)).Skip(1)); 
    
 // skip header of each file
 File.WriteAllLines("Dest-Path", header.Concat(mergedData));

From here

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
Dmitry K.
  • 972
  • 6
  • 16