How to compare Files in two different Folders and perform conditional copying

Asynchronous picture Asynchronous · Dec 7, 2014 · Viewed 10.6k times · Source

I need to copy all the files from one folder (Source) to another folder (Destination). I also need to to compare the two folders and increment a counter that stops at 100 if the content of the two folders are exact match with names only.

I do not need to compare the size of each file, just the names.

This is what I have tried but I am not getting the desire result as described above.

I am referencing some of the resources here: http://msdn.microsoft.com/en-us/library/bb546137.aspx

class Program
{
    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 &&
                    f1.Length == f2.Length);
        }
        public int GetHashCode(System.IO.FileInfo fi)
        {
            string s = String.Format("{0}{1}", fi.Name, fi.Length);
            return s.GetHashCode();
        }
    }

    static void Main(string[] args)
    {

        int i = 1;

        string sourcePath = @"C:\Users\Administrator\Desktop\Source";
        string destinationPath = @"C:\Users\Administrator\Desktop\Dest";

        string fileName = System.IO.Path.GetFileName(sourcePath);

        string source = System.IO.Path.Combine(sourcePath, fileName);
        string destination = System.IO.Path.Combine(destinationPath, fileName);

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(sourcePath);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(destinationPath);

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

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


        string[] files = System.IO.Directory.GetFiles(sourcePath);

        foreach (string s in files)
        {
            fileName = System.IO.Path.GetFileName(s);
            destination = System.IO.Path.Combine(destinationPath, fileName);
            System.IO.File.Copy(s, destination, true);

            FileCompare myFileCompare = new FileCompare();
            bool areIdentical = list1.SequenceEqual(list2, myFileCompare);

            while (areIdentical != true)
            {
                Console.WriteLine(i++);
            }

        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

Answer

mgigirey picture mgigirey · Dec 7, 2014

You can try this solution, synchronize both directories.

class Program
{
    class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
    {
        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 = fi.Name;
            return s.GetHashCode();
        }
    }

    static void Main(string[] args)
    {
        string sourcePath = @"C:\Users\Administrator\Desktop\Source";
        string destinationPath = @"C:\Users\Administrator\Desktop\Dest";

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(sourcePath);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(destinationPath);

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

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

        bool IsInDestination = false;
        bool IsInSource = false;

        foreach (System.IO.FileInfo s in list1)
        {
            IsInDestination = true;

            foreach (System.IO.FileInfo s2 in list2)
            {
                if (s.Name == s2.Name)
                {
                    IsInDestination = true;
                    break;
                }
                else
                {
                    IsInDestination = false;
                }
            }

            if (!IsInDestination)
            {
                System.IO.File.Copy(s.FullName, System.IO.Path.Combine(destinationPath, s.Name), true);
            }
        }

        list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);

        bool areIdentical = list1.SequenceEqual(list2, new FileCompare());

        if (!areIdentical)
        {
            foreach (System.IO.FileInfo s in list2)
            {
                IsInSource = true;

                foreach (System.IO.FileInfo s2 in list1)
                {
                    if (s.Name == s2.Name)
                    {
                        IsInSource = true;
                        break;
                    }
                    else
                    {
                        IsInSource = false;
                    }
                }

                if (!IsInSource)
                {
                    System.IO.File.Copy(s.FullName, System.IO.Path.Combine(sourcePath, s.Name), true);
                }
            }
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}