Last active
April 8, 2021 17:38
-
-
Save PJensen/34171ffb1edf422b9704 to your computer and use it in GitHub Desktop.
Find duplicate files within directory structure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/users/pjensen/ttt/a.txt
/users/pjensen/ttt/c.txt