Skip to content

Instantly share code, notes, and snippets.

@bookuha
Created September 13, 2022 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bookuha/707027336b40663b14c3cb64b039e9ec to your computer and use it in GitHub Desktop.
Save bookuha/707027336b40663b14c3cb64b039e9ec to your computer and use it in GitHub Desktop.
FindDuplicatesInDirectory
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