Saturday, 26 March 2022

Load (merge) all CSV files data into a single CSV file

We are getting n different CSV files with the same structure. Our requirement is to merge the all the CSV files into the one file.

Below are the files

We need to merge all these files into the single CSV file.

We are taking the Script task to merge theses files into the single CSV file.

 

Taking the below variables.

 

Now in script task we are writing the below code.

public void Main()

                                {           

            string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");

            try

            {

                string Scr_Files = Dts.Variables["User::Scr_Files"].Value.ToString();

                string Marge_File_Nm = Dts.Variables["User::Desc_File"].Value.ToString() + "\\" + "Marged_csv_File" + "_" + datetime + ".csv";

 

                int counter = 0;

                string[] fileEntries = Directory.GetFiles(Scr_Files);

                foreach (string fileName in fileEntries)

                {

                       string line;

                       System.IO.StreamReader SourceFile = new System.IO.StreamReader(fileName);

                       StreamWriter sw = null;

                    sw = new StreamWriter(Marge_File_Nm, 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();

                    Dts.TaskResult = (int)ScriptResults.Success;

                }

                Dts.TaskResult = (int)ScriptResults.Success;

             }

             catch (Exception ex)

            {

            Dts.Events.FireError(0, "Fire Error", "An error occurred: " + ex.Message.ToString(), "", 0);

            Dts.TaskResult = (int)ScriptResults.Failure;

            }          

                                }

Now our package is ready to run.

Below is the destination

  

Now we are running this package.

  

See the destination folder.

 

We get the expected result.

1 comment:

  1. This blog really helped me in knowing about logo price in pakistan. Came across this vendor, logo price in pakistan. They are the Best

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts