Click here to Skip to main content
Licence CPOL
First Posted 11 Aug 2008
Views 69,856
Downloads 2,380
Bookmarked 115 times

Duplicate Files Finder

By | 15 Dec 2008 | Article
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!!!!)

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)

About the Author

eRRaTuM

Architect

Morocco Morocco

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionReference to new version Pinmemberabusa21:57 4 Feb '12  
Questionsource code problem Pinmemberarun116814:25 18 Jan '12  
QuestionDuplicate Finder Pinmembervernados6:07 24 Oct '11  
QuestionCrashes when using large number of files? Pinmembermarkus folius12:29 10 Jan '11  
QuestionO(N^2) dependency in the listview? PinmemberSInsanity5:14 5 Dec '10  
GeneralContextSwitchDeadlock was detected PinmemberToothRobber4:45 22 Apr '10  
GeneralMissing ConsoleTestRestorer & Restorer PinmemberMember 28715804:20 4 Apr '10  
GeneralRe: Missing ConsoleTestRestorer & Restorer Pinmemberk2ox7:06 27 Nov '10  
GeneralDuplicate File Finder , Name Finder , Zero Lenght etc Pinmemberstixoffire21:25 22 Dec '08  
GeneralRe: Duplicate File Finder , Name Finder , Zero Lenght etc PinmembereRRaTuM2:37 23 Dec '08  
GeneralRe: Duplicate File Finder , Name Finder , Zero Lenght etc PinmemberBooya10012:35 14 Jan '09  
AnswerRe: Duplicate File Finder , Name Finder , Zero Lenght etc PinmembereRRaTuM14:42 14 Jan '09  
GeneralMy vote of 5. PinmemberLion_King110917:03 22 Dec '08  
GeneralRe: My vote of 5. PinmembereRRaTuM2:51 23 Dec '08  
GeneralDuplicate Files Finder. MD5 Encryption PinmemberHenry Minute5:20 15 Dec '08  
GeneralRe: Duplicate Files Finder. MD5 Encryption PinmembereRRaTuM2:49 23 Dec '08  
GeneralHardcoded path causes RTE PinmemberAt Nel17:43 14 Dec '08  
GeneralRe: Hardcoded path causes RTE PinmembereRRaTuM3:15 15 Dec '08  
Questionthread exception ! any solution?? PinmemberMember 551477510:40 14 Dec '08  
Well, I just dloaded this today and made some small changes quick so i could use it for my own purposes, but now I get this thread exception. I see that there was a post already, but I don't see the solution...
 
--exception occurs in frmMain.cs at---
private void deleteCheckedDuplicateFiles()
{
...
int len = lstFiles.CheckedItems.Count;
...
}
--exception from VS2008
"An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'lstFiles' accessed from a thread other than the thread it was created on."
 
I was hoping to put this to use sometime today yet. Can anyone offer a quick fix? (code is currently using MD5 hashing, not that it matters.)
 
Thanks.
AnswerRe: thread exception ! any solution?? PinmembereRRaTuM3:31 15 Dec '08  
GeneralI like it Pinmember=Xc@libur=17:12 12 Nov '08  
GeneralRe: I like it PinmembereRRaTuM1:52 17 Nov '08  
GeneralDuplicate FIle Name finder Pinmemberpyrodood5:17 9 Sep '08  
AnswerRe: Duplicate FIle Name finder PinmembereRRaTuM16:25 15 Sep '08  
GeneralRe: Duplicate FIle Name finder [modified] Pinmemberpyrodood4:43 16 Sep '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 15 Dec 2008
Article Copyright 2008 by eRRaTuM
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid