Skip to content

Instantly share code, notes, and snippets.

@PJensen
Last active April 8, 2021 17:38
Show Gist options
  • Save PJensen/34171ffb1edf422b9704 to your computer and use it in GitHub Desktop.
Save PJensen/34171ffb1edf422b9704 to your computer and use it in GitHub Desktop.
Find duplicate files within directory structure.
//
// Main.cs
//
// Author:
// pjensen <[email protected]>
using System;
using System.Security.Cryptography;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace Sample
{
class Main
{
static Dictionary<string,List<string>> fileHashes = new Dictionary<string, List<string>>();
public static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("no input args");
return;
}
else if (args.Length == 1)
{
DFS(args [0], delegate(string file)
{
var tmpFileHashBytes = HashAlgorithm.Create("SHA256").ComputeHash(File.Open(file, FileMode.Open));
var tmpFileHashStr = ASCIIEncoding.UTF8.GetString(tmpFileHashBytes);
if (fileHashes.ContainsKey(tmpFileHashStr))
{
fileHashes [tmpFileHashStr].Add(file);
}
else
{
fileHashes.Add(tmpFileHashStr, new List<string> { file });
}
}
);
}
ShowDuplicates();
Console.ReadKey();
}
static void ShowDuplicates()
{
foreach (var k in fileHashes.Keys)
{
if (fileHashes [k].Count > 1)
{
foreach (var v in fileHashes [k])
{
Console.WriteLine(v);
}
}
}
}
static void DFS(string aPath, Action<string> action)
{
foreach (string s in Directory.EnumerateDirectories(aPath))
{
DFS(s, action);
}
foreach (string s in Directory.EnumerateFiles(aPath))
{
action(s);
}
}
}
}
@PJensen
Copy link
Author

PJensen commented Aug 14, 2012

/users/pjensen/ttt/a.txt
/users/pjensen/ttt/c.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment