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#

1 comment:

  1. literature review service plays an important role in helping with writing, especially in academic and research settings. They can be helpful in the following ways: Analyzing and collecting literature A lot of sources are available to experts in literature review services, and they have experience choosing the best works to review. They will assist the author in locating pertinent and reliable sources that will raise the review's overall caliber. Writing a high-quality review: Experts will assist in arranging the chosen sources and structuring the literature review in a way that is logical, cogent, and educational. This will make it simpler for the reader to comprehend the main issues and scientific studies in the area. Respect for academic standards: Literature review services are frequently familiar with the standards and guidelines for preparing a literature review. They will assist in ensuring that your text satisfies all the requirements and is original. Saving time: It can take a lot of time and effort to write a good literature review. This procedure can be significantly accelerated by using literature review services, freeing up the author to focus on other parts of the study or publication. A strong literature review can help you build a stronger foundation for your work by giving you a thorough understanding of the context in which the issue you are researching is situated. Your work will become more credible and convincing as a result. It is crucial to keep in mind that you should consider a company's reputation and credentials when selecting a literature review service. A strong literature study improves your work's quality and helps you comprehend the subject matter better.

    ReplyDelete