Click to See Complete Forum and Search --> : [RESOLVED] Comparing and Updating Text Files


yraen
February 17th, 2009, 10:44 AM
I'm having a problem wrapping my head around something...

I need to have a piece of code that will compare two text files (technically .ini files, but same idea) and then update FileA with any data it's missing from FileB.

Example:
FileA has 3 lines
Bob
Jim
George

FileB has 5 lines
David
Susan
Jack
Jim
Bob

I need to update FileA with the names it is missing without removing any that it already has (or create a new file with combined information but no duplicates).

For some reason, I cannot figure this out without making some really ugly code. (Which hasn't worked right either) :o

Any help is greatly appreciated! :thumb:

eclipsed4utoo
February 17th, 2009, 12:36 PM
this code works, though I am sure there is a better way of doing it...


TextReader fileA = new StreamReader(@"C:\Test\FileA.txt");

StringBuilder fileBNewText = new StringBuilder();

while (fileA.Peek() >= 0)
{
string lineA = fileA.ReadLine();

if (!string.IsNullOrEmpty(lineA))
{
bool found = false;

TextReader fileB = new StreamReader(@"C:\Test\FileB.txt");

while (fileB.Peek() >= 0)
{
string lineB = fileB.ReadLine();

if (lineA == lineB)
{
found = true;
break;
}
}

if (!found)
fileBNewText.AppendLine(lineA);

fileB.Close();
fileB.Dispose();
}
}

fileA.Close();
fileA.Dispose();

TextWriter fileBout = new StreamWriter(@"C:\Test\FileB.txt", true);
fileBout.Write(fileBNewText.ToString(0, fileBNewText.Length - 2));
fileBout.Close();
fileBout.Dispose();

TextReader fileB2 = new StreamReader(@"C:\Test\FileB.txt");
StringBuilder fileANewText = new StringBuilder();

while (fileB2.Peek() >= 0)
{
string lineB = fileB2.ReadLine();

if (!string.IsNullOrEmpty(lineB))
{
bool found = false;

TextReader fileA2 = new StreamReader(@"C:\Test\FileA.txt");

while (fileA2.Peek() >= 0)
{
string lineA = fileA2.ReadLine();

if (lineB == lineA || string.IsNullOrEmpty(lineB))
{
found = true;
break;
}
}

if (!found)
fileANewText.AppendLine(lineB);

fileA2.Close();
fileA2.Dispose();
}
}

fileB2.Close();
fileB2.Dispose();

TextWriter fileAout = new StreamWriter(@"C:\Test\FileA.txt", true);
fileAout.Write(fileANewText.ToString(0, fileANewText.Length -2));
fileAout.Close();
fileAout.Dispose();

yraen
February 17th, 2009, 02:20 PM
Thanks eclipsed!

With some editing and additions, I was able to get it working perfectly. Just couldn't get the basis down :o

Cheers mate! :D

5BabyBulls
February 18th, 2009, 08:45 PM
If it can be got down, keep it up but I need to show people this
an other easier way
Readline function can help read each line into a string collenction
you have 2 collections that you can run 2 loops to compare


5BabyBulls!

eclipsed4utoo
February 18th, 2009, 10:26 PM
If it can be got down, keep it up but I need to show people this
an other easier way
Readline function can help read each line into a string collenction
you have 2 collections that you can run 2 loops to compare


5BabyBulls!

care to show code?