MChalut
MChalut

Reputation: 57

Add text with the 1st line that is written to an external file

Here is my problem...

I am validating rows length from a text file, one row at a time. If the length is <= 2033, it goes for processing. If it > 2033, it goes in a different file.

I would like to be able to add some information at the top of the error file. However, I want to add text only with the first row that will be added. So this is what I have so far:

//Pass the file path and file name to the StreamReader and StreamWriter constructors
StreamReader sr = new StreamReader(inputFile);
StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);

//Read the first line
line = sr.ReadLine();

while (line != null)
{
   int length = line.Length;
   if (length > 2033)
   {
        if // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
        {
            swe.WriteLine("Some records have been rejected at the pre validation phase.");
            swe.WriteLine("Those records will not be included in the process.");
            swe.WriteLine("Please review the records below, fix and re submit if applicable.");
            swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
            swe.WriteLine();
            swe.WriteLine(line);
            count++;
        }
        else
        {
            swe.WriteLine(line);
            count++;
        }
   }
   if (length <= 2033)
   {
        sw.WriteLine(line);
   }
    line = sr.ReadLine(); 
}

Upvotes: 0

Views: 44

Answers (3)

MChalut
MChalut

Reputation: 57

Yep!!! Thanks :-)

bool firstError = true;

           while (line != null)
            {
               int length = line.Length;
               if (length > 2033)
               {
                    if (firstError)
                    {
                        firstError = false; 
                        swe.WriteLine("Some records have been rejected at the pre validation phase.");

                        // etc. etc.

Upvotes: 0

Lee Toffolo
Lee Toffolo

Reputation: 134

using (StreamReader sr = new StreamReader(inputFile))
        {
            using (StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString))
            {
                using (StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString));
                {
                    swe.WriteLine("Some records have been rejected at the pre validation phase.");
                    swe.WriteLine("Those records will not be included in the process.");
                    swe.WriteLine("Please review the records below, fix and re submit if applicable.");
                    swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());

                    while (line = sr.ReadLine() != null)
                    {
                        int length = line.Length;

                        if (length > 2033)
                        {
                            swe.WriteLine();
                            swe.WriteLine(line);
                            count++;
                        }
                        else
                        {
                            sw.WriteLine(line);
                        }
                    }
                }
            }
        }

Upvotes: 1

Brad
Brad

Reputation: 3616

add a bool operator, and only do the extra line if it is first time, then set it so it does not write next time, but always write your line for the bad data

//Pass the file path and file name to the StreamReader and StreamWriter constructors
            StreamReader sr = new StreamReader(inputFile);
            StreamWriter sw = new StreamWriter(Dts.Connections["CE802CleanInput"].ConnectionString);
            StreamWriter swe = new StreamWriter(Dts.Connections["CE802PreValidationErrors"].ConnectionString);

            bool writeHeaderLine = false;

            //Read the first line
            line = sr.ReadLine();

            while (line != null)
            {
                int length = line.Length;
                if (length > 2033)
                {
                    // only do this if it is false/first time
                    if(writeHeaderLine == false) // THIS IS WHERE I WOULD HAVE TO ADD THE CONDITION
                    {
                        swe.WriteLine("Some records have been rejected at the pre validation phase.");
                        swe.WriteLine("Those records will not be included in the process.");
                        swe.WriteLine("Please review the records below, fix and re submit if applicable.");
                        swe.WriteLine("Input file: " + Dts.Connections["CE802Input"].ConnectionString.ToString());
                        swe.WriteLine();                        
                        writeHeaderLine = true;
                    }

                    // always do this
                    swe.WriteLine(line);
                    count++;

                }
                if (length <= 2033)
                {
                    sw.WriteLine(line);
                }
                line = sr.ReadLine();
            }

Upvotes: 0

Related Questions