Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Security.Cryptography;
- var files = Directory.GetFiles(@"G:\My Drive\");
- var groupByLength = files.GroupBy(file => new FileInfo(file).Length)
- .Where(gp => gp.Count() > 1).ToList();
- foreach (var fileByLength in groupByLength)
- {
- var byLength = fileByLength.OrderBy(name => name.Length).ToList();
- string pivotFile = byLength.First();
- var pivot = Md5(pivotFile);
- foreach (var s in byLength.Skip(1))
- {
- if (pivot.SequenceEqual(Md5(s)))
- {
- Console.WriteLine($"{s} is duplicated {pivotFile}");
- File.Delete(s);
- }
- }
- }
- static byte[] Md5(string file)
- {
- using var md5 = MD5.Create();
- using var stream = File.OpenRead(file);
- return md5.ComputeHash(stream);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement