FindDuplicatesInDirectory
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
using System; | |
using System.IO; | |
using System.Linq; | |
static bool CompareFiles(string file1, string file2) | |
{ | |
if (file1 == file2) return true; | |
const int BYTES = 1024 * 10; | |
using FileStream fs1 = File.Open(file1, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); | |
using FileStream fs2 = File.Open(file2, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); | |
byte[] buffer1= new byte[BYTES]; | |
byte[] buffer2= new byte[BYTES]; | |
while (true) | |
{ | |
int len1 = fs1.Read(buffer1, 0, BYTES); | |
int len2 = fs2.Read(buffer2, 0, BYTES); | |
if (!((ReadOnlySpan<Byte>)buffer1).SequenceEqual((ReadOnlySpan<byte>)buffer2)) return false; // SequenceEqual is bad. | |
if (len1 == 0 || len2 == 0) break; | |
} | |
return true; | |
} | |
static void CompareInDirectory(string startingDirectory) | |
{ | |
var dinfo = new DirectoryInfo(startingDirectory); | |
var files = dinfo.EnumerateFiles("",SearchOption.AllDirectories) | |
.Select(finfo => new { finfo.FullName, finfo.Length }) | |
.GroupBy(file => file.Length) | |
.ToDictionary(group => group.Key, group => group.Select(file => file.FullName)); | |
foreach (var entry in files) | |
{ | |
var bucket = entry.Value.ToArray(); | |
for (int i = 0; i < bucket.Length; i++) { | |
for (int k = i + 1; k < bucket.Length; k++) { | |
if (CompareFiles(bucket[i],bucket[k])) { | |
Console.WriteLine(bucket[i] + " and " + bucket[k] + " are the same"); | |
} | |
} | |
} | |
} | |
} | |
CompareInDirectory(@"C:\Program Files (x86)\Steam"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment