2using System.Diagnostics;
5using System.Security.Cryptography;
7using static System.Net.Mime.MediaTypeNames;
13 internal class Program
17 const string DEFAULT_TXT_OUTPUT_PATH =
"C:\\Users\\Alexa\\repos\\duplicate-file-locator\\duplicated-images.txt";
18 const string DUPLICATED_IMAGES_JSON =
"C:\\Users\\Alexa\\repos\\duplicate-file-locator\\duplicated-images.json";
20 const string TEST_JSON =
"C:\\Users\\Alexa\\repos\\duplicate-file-locator\\test.json";
22 static string ByteArrayToString(
byte[] arrInput)
25 StringBuilder sOutput =
new StringBuilder(arrInput.Length);
26 for (i = 0; i < arrInput.Length; i++)
28 sOutput.Append(arrInput[i].ToString(
"X2"));
30 return sOutput.ToString();
33 static string CreateHashOfImage(
string path)
39 Bitmap img =
new Bitmap(path);
45 MemoryStream stream =
new MemoryStream();
46 img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
47 tmpSource = stream.ToArray();
50 tmpHash =
new MD5CryptoServiceProvider().ComputeHash(tmpSource);
52 return ByteArrayToString(tmpHash);
92 static List<string> GetAllFilesInDirectory(
string dir)
94 List<string> files =
new List<string>();
95 if (Directory.Exists(dir))
97 List<string> subdirs = Directory.GetDirectories(dir).ToList<
string>();
98 foreach (
string subdir
in subdirs)
100 files.AddRange(GetAllFilesInDirectory(subdir));
104 files.AddRange(Directory.GetFiles(dir).ToList<
string>());
129 static void RemoveCheckedFileFromList(
string file, List<String> files)
135 static void Main(
string[] args)
138 DuplicatedImageFinder.LoadData(DUPLICATED_IMAGES_JSON);
142 Console.WriteLine(
"(S)earch Folder\n(D)isplay Duplicate Files\n(C)lear Duplicate File Log\n(E)xport Duplicate Log\n(V)erify Duplicated Files\n(H)ash Individual File\n(Q)uit?");
143 string operation = Console.ReadLine();
145 if (operation[0] ==
'S' || operation[0] ==
's')
147 Console.Write(
"Enter Folder to search: ");
148 string dirPath = Console.ReadLine();
149 if (Path.Exists(dirPath))
151 List<string> filePaths = GetAllFilesInDirectory(dirPath);
152 int totalImages = filePaths.Count;
155 int imagesHashed = 0;
157 List<string> hashesFound =
new List<string>();
158 for (
int i = 0; i<filePaths.Count; i++)
160 int progress = (imagesHashed*100) / totalImages;
161 ConsoleUtility.WriteProgressBar(progress,
true);
163 string hash = CreateHashOfImage(filePaths[i]);
165 if (hash !=
string.Empty)
167 if (hashesFound.Contains(hash))
169 DuplicatedImageFinder.AddHash(hash, filePaths[i]);
170 RemoveCheckedFileFromList(filePaths[i], filePaths);
175 hashesFound.Add(hash);
189 ConsoleUtility.WriteProgressBar(100,
true);
190 Console.WriteLine(
"\nTotal Images checked : {0}\nCollating Data now...", imagesHashed);
192 DuplicatedImageFinder.FindOriginals(filePaths, hashesFound);
194 DuplicatedImageFinder.SaveData(DUPLICATED_IMAGES_JSON);
202 Console.WriteLine(
"There are no files in this directory and it's subdirectories, please try again.\n");
208 Console.WriteLine(
"Path does not exist, please try again.\n");
213 else if (operation[0] ==
'D' || operation[0] ==
'd')
215 Console.WriteLine(
"\n{0}\n", DuplicatedImageFinder.DisplayData());
217 else if (operation[0] ==
'C' || operation[0] ==
'c')
220 using (StreamWriter sw = File.CreateText(TEST_JSON))
224 Console.WriteLine(
"Duplicate file cleared.\n");
226 else if (operation[0] ==
'E' || operation[0] ==
'e')
228 using (StreamWriter sw = File.CreateText(DEFAULT_TXT_OUTPUT_PATH))
230 sw.WriteLine(DuplicatedImageFinder.DisplayData());
232 Console.WriteLine(
"Duplicate file exported.\n");
234 else if (operation[0] ==
'V' || operation[0] ==
'v')
236 Console.WriteLine(
"Verify duplicate images found...\n");
237 DuplicatedImageFinder.VerifyDuplicates();
238 DuplicatedImageFinder.SaveData(DUPLICATED_IMAGES_JSON);
240 else if (operation[0] ==
'H' || operation[0] ==
'h')
242 Console.Write(
"Enter path of file to hash: ");
243 string filePath = Console.ReadLine();
244 if (Path.Exists(filePath))
246 string hash = CreateHashOfImage(filePath);
247 Console.WriteLine(
"Hash of image : {0}\n", hash);
251 Console.WriteLine(
"Path does not exist, please try again.\n");
254 else if (operation[0] ==
'Q' || operation[0] ==
'q')
256 Console.WriteLine(
"Thank you, good bye!");
261 Console.WriteLine(
"Invalid input, please try again.\n");