Saturday 19 February 2022

Extract date and Row count from a fix length flat file

We are getting the fix length flat file which having the Header & Trailer row. Header Row is having the Date and the Trailer Row is having the Rows counts. Date and Row Counts are in the fix place.

Below is the file     

Date is staring from (22,8) and row count is staring from (42,5).

We are taking the script task to read the Header & Trailer record and from both line we are subtracting the date and count.

Taking below variables to store the file_path,Row_count and File_date.  

Passing these value in script task.

 

In script task we are writing the below code.

public void Main()

                                {

            // TODO: Add your code here

            //User::File_date,User::File_path,User::Row_count

            string header = "HEADER";

            string trailer = "TRAILER";        

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

            string[] lines = File.ReadAllLines(@textFile);

            foreach (string line in lines)

            {

                if (line.Contains(header))

                {

                    Dts.Variables["User::File_date"].Value= line.Substring(22, 8);                 

                }

                if (line.Contains(trailer))

                {

                    Dts.Variables["User::Row_count"].Value = line.Substring(42, 5);                    

                }

            }

            Dts.TaskResult = (int)ScriptResults.Success;

                                }

 

Now we can use this variable where we need.

We are taking one more script task to see the value.

 

Below is the code.

public void Main()

                                {

            // TODO: Add your code here

            MessageBox.Show("File date : "+Dts.Variables["User::File_date"].Value.ToString()+" Number of rows : "+Dts.Variables["User::Row_count"].Value.ToString());

            Dts.TaskResult = (int)ScriptResults.Success;

                                }

Our package looks like as below.

 

Now running this package.

 

Get the expected result.

Thanks for reading J

Popular Posts