Monday, 13 December 2021

Write the multiple CSV files into a single CSV file using SSIS

 We are getting the number of CSV Files we need to merge the all CSV files into a single CSV file.

Below are the files. 

To merge these files we are using the Script task.

Below is the code to merge these files.

try

            {        

                //Destination file name

                string FileFullPath = @"J:\SSIS1\CSV_Files\Merge_File.csv"; 

                int counter = 0; 

                //Looping through the flat files

                string[] fileEntries = Directory.GetFiles(@"J:\SSIS1\CSV_Files", "*" + ".csv");

                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();

                    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;

            }

Running this package.

Package executed successfully.

See the file below.  

Data in file. 

Thanks.  

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts