C# Question

Comparing .txt files and getting the difference

I'm trying to compare two text files

a.txt
and
b.txt
, I want to get the difference between the two.

a.txt
is the result from yesterday.

b.txt
is the current result.

The tricky thing is that I wonna find out what is missing in "b.txt" compared to "a.txt" even tho there might have been added something new in "b.txt", these new objects needs to be excluded.

The two files is not ordered so what is in index 1 in 'a.txt' can be index 2 in 'b.txt'. I'm comparing string like "mano - mathias rønnow nørtoft".

All I had tried just ends up displaying the new objects aswell.

What I've tried:

string[] File1Lines = File.ReadAllLines(path);
string[] File2Lines = File.ReadAllLines(newPath);
List<string> NewLines = new List<string>();

for (int lineNo = 0; lineNo<File1Lines.Length; lineNo++)
{
if (!String.IsNullOrEmpty(File1Lines[lineNo]) && !String.IsNullOrEmpty(File2Lines[lineNo]))
{
if(String.Compare(File1Lines[lineNo], File2Lines[lineNo]) != 0)
NewLines.Add(File2Lines[lineNo]) ;
}
else if (!String.IsNullOrEmpty(File1Lines[lineNo]))
{
}
else
{
NewLines.Add(File2Lines[lineNo]);
}
}
if (NewLines.Count > 0)
{
File.WriteAllLines(resultpath, NewLines);
}


This just gives me the file merged. Hope I've explained my self correctly.

tried this, why is that not working? it displays nothing.

List<string> a = File.ReadAllLines(path).ToList();
List<string> b = File.ReadAllLines(newPath).ToList();

List<string> copy = new List<string>(a);

foreach (string s in copy)
{
if (b.Contains(s))
{
a.Remove(s);

}
else
{
continue;
}
}
myWriter.WriteLine(a);

Answer

That really depends on how accurate you want the diff to be and how fast you want it to be.

An easy implementation would be to get all lines of both A and B, foreach line in A, if B contains that line then remove that line from both A and B once. What's left would be the lines in A but not in B or wise versa.

Note that this method does not take ordering into consideration, so

Log 1  
C   
B   
A

and

Log 2  
A  
B   
C

are considered identical.

List<string> A;
List<string> B;

List<string> aCopy = new List(A);

foreach(string s in aCopy)
{
    if (B.Contains(s))
    {
        A.Remove(s);
        B.Remove(s);
    }
}

//Whats in A are whats missing in B
//Whats in B are whats missing in A