Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to find the same name files in similar folder and i want to compare these files line by line.

I try this but I could not get a result because of:
C#
if (fileLines1[i] != fileLines2[i])

How can I check the lines?
Code :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace ConsoleApplication67
{
    class Program
    {


        static void Main(string[] args)
        {


            string pathA = @"C:\Users\admin\folder1";
            string pathB = @"C:\Users\admin\folder2";

            System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
            System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);


            IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
            IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);


            FileCompare myFileCompare = new FileCompare();

           

            var queryCommonFiles = list1.Intersect(list2, myFileCompare);
            var queryCommonFiles2 = list2.Intersect(list1, myFileCompare);

            if (queryCommonFiles.Count() > 0)
            {
                String fileName1 = "";
                String fileName2 = "";
                StringBuilder sb = new StringBuilder();

                foreach (var file1 in queryCommonFiles)
                {
                    fileName1 = file1.Name;
                       
                    foreach (var file2 in queryCommonFiles2)
                    {
                        fileName2 = file2.Name;

                        if (fileName1 == fileName2)
                        {

                            string headerLine = String.Format("{0} , {1} , {2} ", "Ln", fileName1, fileName2);
                            sb.AppendLine(headerLine);

                            string[] fileLines1 = File.ReadAllLines(file1.FullName);
                            string[] fileLines2 = File.ReadAllLines(file2.FullName);

                            int count = fileLines1.Length;
                            int j = 1;
                            
                            for (int i = 0; i < count; i++)
                            {
                                string line = "";
                                 
                               if (fileLines1[i] != fileLines2[i])
                                {
                                    line = String.Format("{0} , {1} ,{2}", (j), fileLines1[i], fileLines2[i]); 
                                    sb.AppendLine(line);
                                    
                               }
                            
                                j++;
                               
                            }

                            break;
                        }

                        
                    }

                    sb.AppendLine("\n\n");

                }

                File.WriteAllText(@"C:\Users\admin\Desktop\Report.txt", sb.ToString());
            }

            Console.ReadKey();
            
     
    }
}


class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{
    public FileCompare() { }

    public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
    {
        return (f1.Name == f2.Name);
    }

    public int GetHashCode(System.IO.FileInfo fi)
    {
        string s = String.Format("{0}", fi.Name);
        return s.GetHashCode();
    }
}
Posted
Updated 17-Dec-15 3:37am
v2

In this code

C#
if (fileLines1[i] != fileLines2[i])


if fileLines2 is smaller than fileLines1 then you'll get that error. Let's say there are 5 items in 1 and 3 in 2. When you come to look at the 4th element you'll try and access fileLines2[3] which will throw that exception as there are only 3 items in it. So you'll need to check the second array is big enough before you access it


C#
if (fileLines2.Length > i && fileLines1[i] != fileLines2[i])
 
Share this answer
 
Comments
Volkan Sönmezler 17-Dec-15 10:07am    
Thank you. I am aware of the problems but I can not find solution. I will try this code.
Your issue is that you aren't checking the line entry exists in the fileLines arrays. You could have the situation where i = 10 but there is only 9 lines in file 2.

Try changing your if statement to this:

if(fileLines2.count <= i || (fileLines1[i] != fileLines2[i]))
 
Share this answer
 
Comments
Volkan Sönmezler 17-Dec-15 9:58am    
int count = fileLines1.Length; as defined.

Are you sure about this code(fileLines2.count <= i )?
Pheonyx 17-Dec-15 10:12am    
I have to say, I didn't notice this line here:
line = String.Format("{0} , {1} ,{2}", (j), fileLines1[i], fileLines2[i]);

You would need some logic on that as well so that if i > no of entries in FileLines2 it writes out "Missing" or something similar.

Something like this maybe:

line = String.Format("{0} , {1} ,{2}", (j), fileLines1[i], fileLines2.Count <= i ? fileLines2[i] : "[Does not exist in]");
[no name] 17-Dec-15 10:58am    
find me the one word hola mercillaus hebew
Pheonyx 17-Dec-15 10:03am    
The reason I do that <= check is that if the line doesn't exist then it doesn't match the one in fileLines1, therefore add it to your miss match array. It stops the right hand side of the if executing once you reach the end of fileLines[2] and just puts all the remaining lines in the array regardless.

I'm not 100% if it should be < or <=.

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