Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. public static List<FileInfo> files = new List<FileInfo>();
  2. public static void ListDrive(string drive)
  3. {
  4. try
  5. {
  6. DirectoryInfo di = new DirectoryInfo(drive);
  7. foreach (FileInfo fi in di.GetFiles())
  8. {
  9. files.Add(fi);
  10. }
  11. }
  12. catch (UnauthorizedAccessException)
  13. { }
  14. }
  15. //Find duplicates
  16.  
  17. public static void ListDuplicates()
  18. {
  19. var duplicatedFiles = files.GroupBy(x => new { x.Length }).Where(t => t.Count() > 1).ToList();
  20. Console.WriteLine("Total items: {0}", files.Count);
  21. Console.WriteLine("Probably duplicates {0} ", duplicatedFiles.Count());
  22. StreamWriter duplicatesFoundLog = new StreamWriter("log.txt");
  23. foreach (var filter in duplicatedFiles)
  24. {
  25. duplicatesFoundLog.WriteLine("Probably duplicated item: Name: { 0}, Length: { 1}",
  26. filter.Key.Length);
  27. var items = files.Where(x => x.Length == filter.Key.Length).ToList();
  28. int c = 1;
  29. foreach (var suspected in items)
  30. {
  31. duplicatesFoundLog.WriteLine("{3},{ 0}- { 1}, Creation date { 2}",
  32. suspected.Name, suspected.FullName, suspected.CreationTime, c);
  33. c++;
  34.  
  35. }
  36. duplicatesFoundLog.WriteLine();
  37.  
  38.  
  39.  
  40. }
  41. duplicatesFoundLog.Flush();
  42. duplicatesFoundLog.Close();
  43. }
  44.  
  45. try
  46. {
  47. Console.WriteLine("Enter the path");
  48. string path = Console.ReadLine();
  49.  
  50. ListDrive(path);
  51. ListDuplicates();
  52. Console.ReadLine();
  53. }
  54. catch (Exception e)
  55. {
  56. Console.WriteLine(e.Message);
  57. Console.ReadLine();
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement