Click here to Skip to main content
15,893,190 members
Articles / Programming Languages / C#

Duplicate Files Finder

Rate me:
Please Sign up or sign in to vote.
4.92/5 (50 votes)
15 Dec 2008CPOL2 min read 214.8K   11.9K   138  
A utility to find any duplicate file in your hard drives using MD5 hashing.
using System;
using System.Collections;

namespace DuplicateFinder.Comparers
{
    public class FileListComparer : IComparer
    {
        private string m_FirstFolder = string.Empty;
        
        public int Compare(object x, object y)
        {
            if ((x == null) && (y == null))
            {
                return 0;
            }
            else
            {
                long xLength = ((System.IO.FileInfo)x).Length;
                long yLength = ((System.IO.FileInfo)y).Length;

                int retval = ((System.IO.FileInfo)x).Length.CompareTo(((System.IO.FileInfo)y).Length);
                return retval;
            }
        }

        public FileListComparer(string firstFolder)
        {
            m_FirstFolder = firstFolder;
        }

        public int FileNameComparer(string[] fileInfoA, string[] fileInfoB)
        {
            if (fileInfoA[4] + "\\" + fileInfoA[0] == fileInfoB[4] + "\\" + fileInfoB[0])
                return 0;

            string fiA = fileInfoA[3] + fileInfoA[4].ToLower();
            string fiB = fileInfoB[3] + fileInfoB[4].ToLower();

            if (fileInfoA[3] != fileInfoB[3])
                return fileInfoA[3].CompareTo(fileInfoB[3]);

            if (fileInfoA[4].ToLower().Contains(m_FirstFolder))
                return -1;
            if (fileInfoB[4].ToLower().Contains(m_FirstFolder))
                return 1;

            return fiA[3].CompareTo(fiB[3]);
        }

        public static int FileSizeComparer(System.IO.FileInfo fileA, System.IO.FileInfo fileB)
        {
            return fileA.Length.CompareTo(fileB.Length);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Chief Technology Officer
Morocco Morocco
in his studies, erratum discovered c/c++.he appreciated it.
when he met oracle products, in his job, he fell in love.
he uses c# .net & ms sql.

he created a "f.r.i.e.n.d.s" like soap movie, melting all of the above.
went back in the university.
after he took courses of artificial vision & imagery, he finished his studies with a successful license plate recognition project.

Comments and Discussions