|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Security.Cryptography; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +namespace DEVGIS.DuplicateFileCheck |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static Dictionary<string, MyFileInfo> dictMyFileInfos = null; |
| 13 | + static bool writLog = true; |
| 14 | + static string logPath = string.Empty; |
| 15 | + static List<string> paths = new List<string> {}; |
| 16 | + static void Main(string[] args) |
| 17 | + { |
| 18 | + writLog = false; |
| 19 | + logPath = string.Empty; |
| 20 | + paths = new List<string>(); |
| 21 | + //-p paths |
| 22 | + //-l lpath .outputlogpath |
| 23 | + //-w true false true output log |
| 24 | + if (args != null && args.Length > 0) |
| 25 | + { |
| 26 | + for (int i = 0; i < args.Length; i++) |
| 27 | + { |
| 28 | + switch (args[i].ToLower()) |
| 29 | + { |
| 30 | + case "-p": |
| 31 | + for (int j = i + 1; j < args.Length; j++) |
| 32 | + { |
| 33 | + if (!args[j].StartsWith("-") && Directory.Exists(args[j])) |
| 34 | + { |
| 35 | + paths.Add(args[j]); |
| 36 | + } |
| 37 | + else |
| 38 | + { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + break; |
| 43 | + case "-w": |
| 44 | + try |
| 45 | + { |
| 46 | + if ("true".Equals(args[i + 1].ToLower())) |
| 47 | + { |
| 48 | + writLog = true; |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + writLog = false; |
| 53 | + } |
| 54 | + } |
| 55 | + catch |
| 56 | + { } |
| 57 | + break; |
| 58 | + case "-l": |
| 59 | + try |
| 60 | + { |
| 61 | + string lp = args[i + 1]; |
| 62 | + if (!Directory.Exists(lp)) |
| 63 | + { |
| 64 | + Directory.CreateDirectory(lp); |
| 65 | + } |
| 66 | + |
| 67 | + logPath = lp; |
| 68 | + } |
| 69 | + catch |
| 70 | + { } |
| 71 | + break; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + Start(); |
| 77 | + } |
| 78 | + |
| 79 | + private static void Start() |
| 80 | + { |
| 81 | + DateTime time1 = DateTime.Now; |
| 82 | + Console.WriteLine($"{time1.ToString()}:Started!"); |
| 83 | + dictMyFileInfos = new Dictionary<string, MyFileInfo>(); |
| 84 | + logPath = Path.Combine(logPath, time1.ToString("yyyyMMddHHmmss") + ".log"); |
| 85 | + |
| 86 | + foreach (string path in paths) |
| 87 | + { |
| 88 | + VisitPath(path); |
| 89 | + } |
| 90 | + DateTime time2 = DateTime.Now; |
| 91 | + Console.WriteLine($"{time2.ToString()}:Completed!"); |
| 92 | + Console.WriteLine($"TotalCount:{dictMyFileInfos.Count}"); |
| 93 | + Console.WriteLine("Result-------------------------------------------------------------------------------"); |
| 94 | + int sameindex = 0; |
| 95 | + StringBuilder sb = new StringBuilder(); |
| 96 | + foreach (var value in dictMyFileInfos.Values) |
| 97 | + { |
| 98 | + if (value.DuplicateFiles != null) |
| 99 | + { |
| 100 | + sameindex++; |
| 101 | + string s = sameindex + ":" + value.Name; |
| 102 | + foreach (var d in value.DuplicateFiles) |
| 103 | + { |
| 104 | + s += " And " + d.Name; |
| 105 | + } |
| 106 | + Console.WriteLine(s); |
| 107 | + sb.AppendLine(s); |
| 108 | + } |
| 109 | + } |
| 110 | + Console.WriteLine("Result-------------------------------------------------------------------------------"); |
| 111 | + sb.AppendLine($"TotalDuplicateCounts:{sameindex}"); |
| 112 | + sb.AppendLine($"TotalUsedSeconds:{(time2 - time1).TotalSeconds}"); |
| 113 | + Console.WriteLine($"Done! File name is {logPath}"); |
| 114 | + Console.WriteLine("Press any key to exit!"); |
| 115 | + File.WriteAllText(logPath, sb.ToString()); |
| 116 | + Console.Read(); |
| 117 | + } |
| 118 | + private static void ShowLog(string message) |
| 119 | + { |
| 120 | + |
| 121 | + } |
| 122 | + private static void VisitPath(string Path) |
| 123 | + { |
| 124 | + Console.WriteLine($"{DateTime.Now.ToString()}:Visiting {Path}"); |
| 125 | + foreach (var file in Directory.GetFiles(Path)) |
| 126 | + { |
| 127 | + var myfileinfo = GetMyFileInfo(file); |
| 128 | + if (dictMyFileInfos.ContainsKey(myfileinfo.Key)) |
| 129 | + { |
| 130 | + if (dictMyFileInfos[myfileinfo.Key].DuplicateFiles == null) |
| 131 | + { |
| 132 | + dictMyFileInfos[myfileinfo.Key].DuplicateFiles = new List<MyFileInfo>(); |
| 133 | + } |
| 134 | + dictMyFileInfos[myfileinfo.Key].DuplicateFiles.Add(myfileinfo); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + dictMyFileInfos.Add(myfileinfo.Key, myfileinfo); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + //visit the paths |
| 143 | + foreach (var dir in Directory.GetDirectories(Path)) |
| 144 | + { |
| 145 | + VisitPath(dir); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static MyFileInfo GetMyFileInfo(string FileName) |
| 150 | + { |
| 151 | + if (File.Exists(FileName)) |
| 152 | + { |
| 153 | + return new MyFileInfo { Key= GetMD5WithFilePath (FileName),Name= FileName }; |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + return null; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + static MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); |
| 162 | + static public string GetMD5WithFilePath(string filePath) |
| 163 | + { |
| 164 | + FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); |
| 165 | + byte[] hash_byte = md5.ComputeHash(file); |
| 166 | + string str = System.BitConverter.ToString(hash_byte); |
| 167 | + str = str.Replace("-", ""); |
| 168 | + return str; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments