Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to do something Like this, Comparing File1 with File2 and write Incremental data into another file. But the point is here I want to exclude Header row while Comparing. I trying to do this in C#

Below is the code I have tried, I am using this code in SSIS Script task.

Immediate attention will be aprreciated..!!

Thanks in Advance
Pradeep Kumar T

What I have tried:

C#
foreach (string CFile in Directory.EnumerateFiles(CurrentWkFile))
                {
                    WriteLogFile("Step 2 : Checking " + CFile + " from path " + CurrentWkFile);

                    //var pFile = Directory.GetFiles(PreviousWkFile, CFile, SearchOption.AllDirectories).FirstOrDefault();

                    String[] Prevwk = File.ReadAllLines(Path.Combine(PreviousWkFile, Path.GetFileName(CFile)));
                    Prevwk.Skip(0);
                    String[] CurWk = File.ReadAllLines(Path.Combine(CurrentWkFile, Path.GetFileName(CFile)));
                    CurWk.Skip(0);



                    if (File.Exists(PreviousWkFile + "\\" + Path.GetFileName(CFile)))
                    {
                        WriteLogFile("Step 3 : Comparing file" + Path.GetFileName(CFile) + "with previous week file");
                        IEnumerable<string> Exprevwk = Prevwk.Skip(0);
                        IEnumerable<string> ExCurrwk = CurWk.Skip(0);

                        IEnumerable<string> CurrWkOnly = (ExCurrwk.Skip(0).Except(Exprevwk.Skip(0))).Skip(0);
                        WriteLogFile("Step 4 : File Compared ready to write incremental record in the file " + CFile);
                        File.WriteAllLines(Path.Combine(ToProcessFile, Path.GetFileName(CFile)), CurrWkOnly.Skip(0));
                        WriteLogFile("step 5 :Incremental data written in the file " + CFile + "placed in the folder path" + ToProcessFile);

                    }
                    else
                    {
                        IEnumerable<string> CurrWeek = CurWk;
                        WriteLogFile("Crazy step :In Prevoius week " + Path.GetFileName(CFile) + "not exists in the path" + PreviousWkFile);
                        File.WriteAllLines(Path.Combine(ToProcessFile, Path.GetFileName(CFile)), CurrWeek);

                    }

                }
Posted
Updated 22-Mar-16 23:55pm
v2
Comments
FARONO 23-Mar-16 4:29am    
Can't you use a "foreach" int x from 1 (actually line 2, zero-based) to Prevwk.Length-1 ? And then compare Prevwk[x] with Curwk[x]?
I'm not sure what you mean with the incremental file..
Check first which has more lines, prev or cur, use the most.
pradeep kumar 23-Mar-16 10:22am    
Yes My requirement is Thomas Nielsen reply only..!!

Thanks for your reply..!!
Thomas Nielsen - getCore 23-Mar-16 5:00am    
If I understand you correctly, you want to compare two files, line by line, and write that which is new into another file containing the increment?
Both files containing header rows, which chould be ignored ... also in the result file??
pradeep kumar 23-Mar-16 10:23am    
Yes correct..!! Please post your ideas on this..!!

Thanks
Thomas Nielsen - getCore 23-Mar-16 10:39am    
super, see my solution suggestion below :)

1 solution

Seems pretty straight forward, i wonder what the problem is ... you're targeting framework 3.5 ok, but SSIS what does that have to do with the matter at hand?

Anyway here's my five cents on a simple program that does that in 3.5

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ComparingOfFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            Write("Welcome to file compare program utility, it will generate a file and add some lines, then it will copy it and add more. Finally compare the two and write the added content to a new file.\r\n");

            try
            {
                var parts = Assembly.GetExecutingAssembly().Location.Split(@"\".ToCharArray());
                parts[parts.GetUpperBound(0)] = "";
                var basePath = string.Join(@"\", parts);

                string fileOne = Path.Combine(basePath, "fileone.txt");
                string fileTwo = Path.Combine(basePath, "filetwo.txt");
                string fileResult = Path.Combine(basePath, "fileresult.txt");

                int columns = 8;
                string valuerange = "abcdefghijklmnopqrstuvwxyz";
                string seperator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
                var rnd = new Random((int) DateTime.Now.Ticks);
                Write("Deling files if existing");
                foreach(var file in new []{fileOne, fileTwo, fileResult})
                    if(File.Exists(file))
                        File.Delete(file);
                Write("Making log file one");
                using (var fs = new FileStream(fileOne, FileMode.Create))
                {
                    var writer = new StreamWriter(fs, Encoding.UTF8);
                    for (int x = 0; x < 10; x++)
                        AddColumnsOfRandomWords(columns, rnd, valuerange, seperator, writer);
                    writer.Flush();
                    writer.Close();
                }
                Write("Copying to logfile two and adding some lines");
                File.Copy(fileOne, fileTwo);
                using (var fs = new FileStream(fileTwo, FileMode.Append))
                {
                    var writer = new StreamWriter(fs, Encoding.UTF8);
                    for (int x = 0; x < 5; x++)
                        AddColumnsOfRandomWords(columns, rnd, valuerange, seperator, writer);
                    writer.Flush();
                    writer.Close();
                }


                Write("Comparing the two files");
                var sbDifference = new StringBuilder();
                var fileOneContentRows = new List<string>();
                var fileTwoContentRows = new List<string>();

                using (var fs = new FileStream(fileOne, FileMode.Open, FileAccess.Read))
                {
                    var reader = new StreamReader(fs, Encoding.UTF8);
                    while(!reader.EndOfStream)
                        fileOneContentRows.Add(reader.ReadLine());
                    reader.Close();
                }
                using (var fs = new FileStream(fileTwo, FileMode.Open, FileAccess.Read))
                {
                    var reader = new StreamReader(fs, Encoding.UTF8);
                    while (!reader.EndOfStream)
                        fileTwoContentRows.Add(reader.ReadLine());
                    reader.Close();
                }

                using (var fs = new FileStream(fileResult, FileMode.CreateNew))
                {
                    int idx = 1;
                    while (idx < fileOneContentRows.Count())
                    {
                        if (fileOneContentRows[idx].Equals(fileTwoContentRows[idx]))
                        {
                            idx++;
                            continue;
                        }
                        throw new DataException("OMFG! the logs from last weeks have been changed this week!");
                    }
                    if (fileOneContentRows.Count() >= fileTwoContentRows.Count())
                    {
                        sbDifference.AppendLine("Log error, the content of the logfile appears to have decreased between one: " + fileOne + " and two: " + fileTwo);
                    } else
                    {
                        for (int z = idx; z < fileTwoContentRows.Count();z++ )
                        {
                            sbDifference.AppendLine(fileTwoContentRows[z]);
                        }
                    }
                    var writer = new StreamWriter(fs, Encoding.UTF8);
                    writer.Write(sbDifference.ToString());
                    writer.Flush();
                    writer.Close();
                }
                Write("Done");
            } catch (Exception ex)
            {
                Write("Exception occured:\r\n" + ex.ToString());
            }
            Write("\r\nPress ENTER to exit");
            Console.ReadLine();
        }

        private static void AddColumnsOfRandomWords(int columnsToAdd, Random rnd, string valuerange, string seperator, StreamWriter writer)
        {
            for (int i = 0; i < columnsToAdd; i++)
            {
                int length = rnd.Next(1, 20);
                var word = "";
                for (int y = 0; y < length; y++)
                    word += valuerange.Substring(rnd.Next(valuerange.Length - 1), 1);
                if (i < (columnsToAdd - 1))
                    word += seperator;
                writer.Write(word);
            }
            writer.Write("\r\n");
        }

        private static void Write(string message)
        {
            Console.WriteLine(message);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900