Click here to Skip to main content
13,448,005 members (53,244 online)
Click here to Skip to main content
Add your own
alternative version


39 bookmarked
Posted 24 May 2012

Find Duplicate Files and Delete Them

, 1 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Application for seaching similar kinds of files in different folders.


Actually I have created this application to search for similar songs on my computer. Using it I found multiple songs in different folders, but this application can be used for searching any kind similar files. Here we will also see how we can search for the similar files from the specified folder. As per the suggestions i have also updated the tool to search from file length as well as through file hash value. if you do the file length there are very rare chances of finding similar file even if they are different. but in case of file hash, there is no chances of grouping different fine and consider it as single one. File matching through hash will take more time then comparison through file length.

Using the Code

We have used C# and WPF for the application development. My background is with Windows forms so my WPF code may not be optimized. Suggestions will be welcome.

First of all we need to find all the files from the specified folder. To fulfill this requirement I have created one recursive function for getting all the files from specified folder. The below method will give you all the files (in form of List<FileAttrib>) containing in the Given Directory (we need to pass DirectoryInfo object)

/// <summary>
/// Method to get all the files from given folder and subfolders.
/// </summary>
/// <param name="dinfo">DirectoryInfo object for the directory.</param>
/// <returns>Gives list of FileAttrib object with all files</returns>
private List<FileAttrib> GetFiles(DirectoryInfo dinfo)
    List<FileAttrib> files = new List<FileAttrib>();

    if (this.searchExtension == "*")
        files.AddRange(dinfo.GetFiles().Select(s => this.ConvertFileInfo(s)));
        files.AddRange(dinfo.GetFiles().Where(g => g.Extension.ToLower() == 
           this.searchExtension.ToLower())).Select(s => this.ConvertFileInfo(s)));

    foreach (var directory in dinfo.GetDirectories())
    return files;

We have created one class to store FileInfo object with required information. FileAttrib is the class used to store information. Here is the FileAttrib class code.

public class FileAttrib
    public string fileName { get; set; }
    public string filePath { get; set; }
    public string fileImpression { get; set; }     
    public double fileLength { get; set; }

We have stored only FileName, FilePath and FileImpression (it defines your file identity.) We have created one function that Converts our FileInfo object into FileAttrib type. Below is the code for conversion.

/// <summary>
/// Convertor for converting FileInfo object into FileAttrib
/// </summary>
/// <param name="finfo">Original FileInfo object</param>
/// <returns>Generated FileAttrib object</returns>
private FileAttrib ConvertFileInfo(FileInfo finfo)
    return new FileAttrib
        fileName = finfo.Name,
        filePath = finfo.FullName,
        fileImpression = isHashSearch ? this.FileToMD5Hash(finfo.FullName) : null,
        fileLength = finfo.Length

You can see one FileToMD5Hash function to generate FileImpression information from the given file location. Below is the code for generating file impression from the file location.

/// <summary>
/// Function to get file impression in form of string from a file location.
/// </summary>
/// <param name="_fileName">File Path to get file impression.</param>
/// <returns>Byte Array</returns>
private string FileToMD5Hash(string _fileName)
    using (var stream = new BufferedStream(File.OpenRead(_fileName), 1200000))
        SHA256Managed sha = new SHA256Managed();
        byte[] checksum = sha.ComputeHash(stream);
        return BitConverter.ToString(checksum).Replace("-", string.Empty);

Here is a LINQ query for grouping the result. This may also help you in other projects.

e.Result = this.GetFiles(dinfo)
                            .GroupBy(i => i.fileLength)
                            .Where(g => g.Count() > 1)
                            .SelectMany(list => list)

All other code is very simple to understand. Here you can find the code of the C#. I have not explained the WPF parts (as I am not expert in it). All your suggestions/complaints/appreciations are welcome. Hope this application will be helpful to you.

Step 1: Select folder to search similar files.

Step 2: Select folder for searching similar files.

Step 3: Choose search from either FileLength or from FileHash(more time consuming)

Step 4: Click on the Start button to search similar file in a group.

Step 5: Delete selected file using context menu.

In future i will keep updating this Utility. Keep posting your suggestiong/Query. you can also follow this project on codeplex @ FindDuplicateFile_CodePlex


31 Aug 2012 - Added Multiple File delete functionality

19 Jun 2012 - Added Search option through file length

25 May 2012 - Initial Release


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


About the Author

Software Developer (Senior)
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Questionsir Pin
bryanjaysena@yahoo.com4-Jan-17 21:10
memberbryanjaysena@yahoo.com4-Jan-17 21:10 
Questionhello sir Pin
Member 1265517928-Oct-16 16:45
memberMember 1265517928-Oct-16 16:45 
AnswerRe: hello sir Pin
@AmitGajjar31-Oct-16 5:48
professional@AmitGajjar31-Oct-16 5:48 
QuestionSearch by content Pin
Member 1173577224-Aug-16 23:53
memberMember 1173577224-Aug-16 23:53 
AnswerRe: Search by content Pin
@AmitGajjar2-Sep-16 11:41
professional@AmitGajjar2-Sep-16 11:41 
Questionnice code - my suggestion Pin
Member 1179891214-Feb-16 12:17
memberMember 1179891214-Feb-16 12:17 
AnswerRe: nice code - my suggestion Pin
@AmitGajjar15-Feb-16 3:55
professional@AmitGajjar15-Feb-16 3:55 
GeneralRe: nice code - my suggestion Pin
Member 1179891216-Feb-16 14:20
memberMember 1179891216-Feb-16 14:20 
AnswerDelete Duplicate Files Pin
Member 1112019830-Sep-14 0:33
memberMember 1112019830-Sep-14 0:33 
QuestionDuplicate files tool Pin
Member 1021093418-Aug-13 23:03
memberMember 1021093418-Aug-13 23:03 
QuestionMessage Closed Pin
14-Jun-13 5:24
membermark bun14-Jun-13 5:24 
NewsMessage Closed Pin
10-Dec-12 21:44
memberriver raid10-Dec-12 21:44 
Questionsaymahayen Pin
Member 960575916-Nov-12 9:47
memberMember 960575916-Nov-12 9:47 
General[My vote of 1] My 1 for not keeping the code up-to-date Pin
Andreas Gieriet20-Jun-12 13:19
memberAndreas Gieriet20-Jun-12 13:19 
GeneralRe: [My vote of 1] My 1 for not keeping the code up-to-date Pin
amitgajjar20-Jun-12 18:09
memberamitgajjar20-Jun-12 18:09 
GeneralRe: [My vote of 1] My 1 for not keeping the code up-to-date Pin
Andreas Gieriet20-Jun-12 20:36
memberAndreas Gieriet20-Jun-12 20:36 
GeneralRe: [My vote of 1] My 1 for not keeping the code up-to-date Pin
amitgajjar20-Jun-12 20:38
memberamitgajjar20-Jun-12 20:38 
Bug[My vote of 1] Exception Pin
Omar Gamil19-Jun-12 22:40
memberOmar Gamil19-Jun-12 22:40 
GeneralRe: [My vote of 1] Exception Pin
amitgajjar19-Jun-12 22:44
memberamitgajjar19-Jun-12 22:44 
GeneralRe: [My vote of 1] Exception Pin
amitgajjar19-Jun-12 22:47
memberamitgajjar19-Jun-12 22:47 
GeneralMy vote of 1 Pin
Lorenzo Gatti18-Jun-12 4:54
memberLorenzo Gatti18-Jun-12 4:54 
GeneralRe: My vote of 1 Pin
amitgajjar18-Jun-12 18:17
memberamitgajjar18-Jun-12 18:17 
GeneralRe: My vote of 1 Pin
amitgajjar19-Jun-12 3:01
memberamitgajjar19-Jun-12 3:01 
QuestionVery Useful Application Pin
Jean Paul V.A13-Jun-12 8:47
memberJean Paul V.A13-Jun-12 8:47 
AnswerRe: Very Useful Application Pin
amitgajjar14-Jun-12 18:19
memberamitgajjar14-Jun-12 18:19 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180318.3 | Last Updated 1 Sep 2012
Article Copyright 2012 by @AmitGajjar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid