Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Article

Duplicate Files Finder

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

DuplicateFinder.old.jpg

Search results

DuplicateFinder_deleted.JPG

File deleted

Introduction

Once a year, I do that terrific job of cleaning files I created or downloaded on my drives. The last time I tried to do it, it was such a fastidious task that I thought of doing that thing semi-automatically. I needed some free utility that could find duplicate files, but I found none that corresponded to my needs. I decided to write one.

Background

The CRC calculation method is available here. I use the MD5 hashing provided by the standard libraries. I added an event to the MD5 computing method so as to get a hashing progression, it is a thread that reads the stream position while the MD5 computing method is reading the same stream.

Using the code

The utility uses two main classes, DirectoryCrawler and Comparers. The use is obvious :) Please notice that instead of iterating through a list list.count X list.count times, DuplicateFinder uses a Hashtable that contains the pair <size,count>. Once populated, all files with count =1 will be removed: (Very much faster!!!!)

C#
int len = filesToCompare.Length;
List<long> alIdx = new List<long>();
System.Collections.Hashtable HLengths = new System.Collections.Hashtable();
foreach (FileInfo fileInfo in filesToCompare)
{
    if (!HLengths.Contains(fileInfo.Length))
        HLengths.Add(fileInfo.Length, 1);
    else
        HLengths[fileInfo.Length] = (int)HLengths[fileInfo.Length] + 1;
}
foreach (DictionaryEntry hash in HLengths)
    if ((int)hash.Value == 1)
    {                    
        alIdx.Add((long)hash.Key);
        setText(stsMain, string.Format("Will remove File with size {0}", hash.Key));
    }
FileInfo[] fiZ = new FileInfo[len - alIdx.Count];
int j = 0;
for (int i = 0; i < len; i++)
{
    if (!alIdx.Contains(filesToCompare[i].Length))
        fiZ[j++] = filesToCompare[i];
}
return fiZ;

Points of interest

  • (Done) Optimizes file moving, UI may be unresponsive while moving big files :(.
  • (Useless, my MD5 is better ^_^) Add options to choose between CRC32 and MD5 hashing.
  • Maybe use an XML configuration file. At this time, moving duplicate files to D:\DuplicateFiles (which is hard coded, viva Microsoft!) and skipping that folder during scanning is sufficient to me.
  • Don't forget that your posts make POIs.
  • (Done): Code an event enabled MD5 hashing class that would report hashing progression, imagine hashing a 10 GB file!

History

  • v0.2
    • Optimized duplicates retrieving (duplicate sizes and duplicate hashes).
    • Added Move to Recycle Bin.
    • Added file size criteria.
    • Files to delete info updated for every check/uncheck in listview.
    • Added colors and fonts to UI.
    • Debug enabled sources (#if DEBUG synchronous #else threaded).
    • Added List<Fileinfo> and List<string[]> instead of using array lists.
    • MD5 hashing is used instead of CRC32 (supercat9).
    • Added Skip Source Folder option.
    • Added Drop SubFolder.
    • Some optimizations...
  • v0.1
    • First time publishing. Waiting for bug reports :)

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

 
QuestionLove it!, but ... Pin
PJCTX130-Mar-18 14:53
PJCTX130-Mar-18 14:53 
PraiseDocumentation Pin
MpsheG29-Jan-17 22:54
MpsheG29-Jan-17 22:54 
QuestionDuplicate Files Deleter Pin
Samantha_Chloe18-Aug-16 4:10
Samantha_Chloe18-Aug-16 4:10 
QuestionVery good job and many thanks Pin
dede33325-Dec-15 22:49
professionaldede33325-Dec-15 22:49 
AnswerRe: Very good job and many thanks Pin
eRRaTuM11-Feb-16 10:22
eRRaTuM11-Feb-16 10:22 
Questionapp hangs in watchFinished() Pin
Art Psi14-May-15 20:44
Art Psi14-May-15 20:44 
QuestionDelete Duplicate Files Pin
bardy114-Oct-14 19:55
bardy114-Oct-14 19:55 
AnswerRe: Delete Duplicate Files Pin
eRRaTuM19-Nov-14 15:08
eRRaTuM19-Nov-14 15:08 
GeneralMy vote of 5 Pin
Midax31-Oct-13 3:39
Midax31-Oct-13 3:39 
GeneralRe: My vote of 5 Pin
eRRaTuM28-May-14 3:43
eRRaTuM28-May-14 3:43 
Generaldup0licate folder Pin
tirmizi1010-Dec-12 3:33
tirmizi1010-Dec-12 3:33 
where can i specify a duplicate folder(where is the tool going to copy duplicates files)?
thanks
GeneralRe: dup0licate folder Pin
eRRaTuM28-May-14 3:42
eRRaTuM28-May-14 3:42 
Questionsaymahayen Pin
Member 960575916-Nov-12 9:51
Member 960575916-Nov-12 9:51 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey6-Jul-12 19:55
professionalManoj Kumar Choubey6-Jul-12 19:55 
GeneralRe: My vote of 5 Pin
eRRaTuM28-May-14 3:40
eRRaTuM28-May-14 3:40 
QuestionReference to new version Pin
abusa4-Feb-12 21:57
abusa4-Feb-12 21:57 
Questionsource code problem Pin
arun116818-Jan-12 14:25
arun116818-Jan-12 14:25 
QuestionDuplicate Finder Pin
vernados24-Oct-11 6:07
vernados24-Oct-11 6:07 
QuestionCrashes when using large number of files? Pin
markus folius10-Jan-11 12:29
markus folius10-Jan-11 12:29 
AnswerRe: Crashes when using large number of files? Pin
eRRaTuM28-May-14 3:46
eRRaTuM28-May-14 3:46 
QuestionO(N^2) dependency in the listview? Pin
SInsanity5-Dec-10 5:14
SInsanity5-Dec-10 5:14 
AnswerRe: O(N^2) dependency in the listview? Pin
eRRaTuM28-May-14 3:25
eRRaTuM28-May-14 3:25 
GeneralContextSwitchDeadlock was detected Pin
ToothRobber22-Apr-10 4:45
professionalToothRobber22-Apr-10 4:45 
GeneralMissing ConsoleTestRestorer & Restorer Pin
Shiku-578414-Apr-10 4:20
professionalShiku-578414-Apr-10 4:20 
GeneralRe: Missing ConsoleTestRestorer & Restorer Pin
k2ox27-Nov-10 7:06
k2ox27-Nov-10 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.