C# - How to write multiple CSV files to Single CVS File in C Sharp

Scenario: Download Script

You are working as C# developer, you need to write a program that should read multiple csv files from a folder and write to single csv file. All the input files has the same number of columns.

Sample files from Source folder as shown below.
How to write multiple flat files to single flat file in C#

The below C# code can be used to write multiple files to single file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlClient;
using System.Data;


namespace TechBrothersIT.com_CSharp_Tutorial
{
    class Program
    {
        static void Main(string[] args)
        {

            string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string LogFolder = @"C:\Log\";
            try
            {

                //Declare Variables and provide values
                string DestinationFolder = @"C:\Destination\"; //Provide Destination folder path
                string FileDelimiter = ","; //Provide file delimiter such as comma or pipe for source files
                string FileExtension = ".txt"; //Provide file extension such as .txt or .csv for source file
                string SourceFolder = @"C:\Source\"; //Provide the source folder where your input files are present
                string FileName = "Customer_Final"; //Provide the file name in which you like to write all files
                string DestinationFileExtension = ".txt"; //Provide Extension for destination file

                //Building Destination file name
                string FileFullPath = DestinationFolder + "\\" + FileName + "_" + datetime + DestinationFileExtension;

                int counter = 0;

                //Looping through the flat files 
                string[] fileEntries = Directory.GetFiles(SourceFolder, "*" + FileExtension);
                foreach (string fileName in fileEntries)
                {
                    string line;
                    System.IO.StreamReader SourceFile =
                    new System.IO.StreamReader(fileName);

                    StreamWriter sw = null;
                    sw = new StreamWriter(FileFullPath, true);

                    int linecnt = 0;
                    while ((line = SourceFile.ReadLine()) != null)
                    {
                        //Write only the header from first file
                        if (counter == 0 && linecnt == 0)
                        {
                            sw.Write(line);
                            sw.Write(sw.NewLine);

                        }
                        //Write data records from flat files
                        if (linecnt != 0)
                        {
                            sw.Write(line);
                            sw.Write(sw.NewLine);
                        }
                        linecnt++;
                        counter++;
                    }

                    sw.Close();
                 
                }

            }
            catch (Exception exception)
            {
                // Create Log File for Errors
                using (StreamWriter sw = File.CreateText(LogFolder
                    + "\\" + "ErrorLog_" + datetime + ".log"))
                {
                    sw.WriteLine(exception.ToString());
                   
                }

            }

        }
    }
}


Once I executed above script, it read all the input files and wrote to single csv file as shown below.
How to write multiple CSV files to Single CSV File in C#

No comments:

Post a Comment