Duplicate File Locator 1.0
Loading...
Searching...
No Matches
Program.cs
Go to the documentation of this file.
1using System;
2using System.Diagnostics;
3using System.Drawing;
4
5using System.Security.Cryptography;
6using System.Text;
7using static System.Net.Mime.MediaTypeNames;
8
9
10namespace ProofOfConcept
11{
12
13 internal class Program
14 {
15 // Function below usies code from the MD5 Comparison.
16
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";
19
20 const string TEST_JSON = "C:\\Users\\Alexa\\repos\\duplicate-file-locator\\test.json";
21
22 static string ByteArrayToString(byte[] arrInput)
23 {
24 int i;
25 StringBuilder sOutput = new StringBuilder(arrInput.Length);
26 for (i = 0; i < arrInput.Length; i++)
27 {
28 sOutput.Append(arrInput[i].ToString("X2"));
29 }
30 return sOutput.ToString();
31 }
32
33 static string CreateHashOfImage(string path)
34 {
35 //Ref: https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/csharp/language-compilers/compute-hash-values
36 //Ref: https://www.sitepoint.com/community/t/bitmap-to-byte-array-in-c/1877
37 try
38 {
39 Bitmap img = new Bitmap(path);
40
41 byte[] tmpSource;
42 byte[] tmpHash;
43
44 //Create a byte array from source data.
45 MemoryStream stream = new MemoryStream();
46 img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
47 tmpSource = stream.ToArray();
48
49 //Compute hash based on source data.
50 tmpHash = new MD5CryptoServiceProvider().ComputeHash(tmpSource);
51
52 return ByteArrayToString(tmpHash);
53 }
54 catch
55 {
56 return string.Empty;
57 }
58
59 }
60
61 //static bool CompareFiles(string file1, string file2)
62 //{
63 // Console.WriteLine(file2);
64 // Bitmap image1 = new Bitmap(file1);
65 // Bitmap image2 = new Bitmap(file2);
66
67 // if (image1.Width != image2.Width || image1.Height != image2.Height)
68 // {
69 // return false;
70 // }
71
72 // //for (int i=0; i < image1.Width; i++)
73 // //{
74 // // for (int j = 0; j < image1.Height; j++)
75 // // {
76 // // var img1_ref = image1.GetPixel(i, j);
77 // // var img2_ref = image2.GetPixel(i, j);
78
79 // // if (img1_ref != img2_ref)
80 // // {
81 // // return false;
82 // // }
83 // // }
84 // //}
85
86 // string image1Hash = CreateHashOfImage(image1);
87 // string image2Hash = CreateHashOfImage(image2);
88
89 // return image1Hash == image2Hash;
90 //}
91
92 static List<string> GetAllFilesInDirectory(string dir)
93 {
94 List<string> files = new List<string>();
95 if (Directory.Exists(dir))
96 {
97 List<string> subdirs = Directory.GetDirectories(dir).ToList<string>();
98 foreach (string subdir in subdirs)
99 {
100 files.AddRange(GetAllFilesInDirectory(subdir));
101 }
102
103 // Add the files in the current directory
104 files.AddRange(Directory.GetFiles(dir).ToList<string>());
105 }
106 return files;
107 }
108
109 //static DuplicatedImage FindDuplicateImage(string ogFile, List<string> files)
110 //{
111 // DuplicatedImage duplicatedImage = null;
112 // foreach (string file in files)
113 // {
114 // if(CompareFiles(ogFile, file))
115 // {
116 // if (duplicatedImage == null)
117 // {
118 // duplicatedImage = new DuplicatedImage(ogFile, file);
119 // }
120 // else
121 // {
122 // duplicatedImage.AddDuplicate(file);
123 // }
124 // }
125 // }
126 // return duplicatedImage;
127 //}
128
129 static void RemoveCheckedFileFromList(string file, List<String> files)
130 {
131 files.Remove(file);
132 }
133
134
135 static void Main(string[] args)
136 {
137
138 DuplicatedImageFinder.LoadData(DUPLICATED_IMAGES_JSON);
139
140 while (true)
141 {
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();
144 {
145 if (operation[0] == 'S' || operation[0] == 's')
146 {
147 Console.Write("Enter Folder to search: ");
148 string dirPath = Console.ReadLine();
149 if (Path.Exists(dirPath))
150 {
151 List<string> filePaths = GetAllFilesInDirectory(dirPath);
152 int totalImages = filePaths.Count;
153 if (totalImages > 0)
154 {
155 int imagesHashed = 0;
156 // Create a list of every hash found
157 List<string> hashesFound = new List<string>();
158 for (int i = 0; i<filePaths.Count; i++)
159 {
160 int progress = (imagesHashed*100) / totalImages;
161 ConsoleUtility.WriteProgressBar(progress, true);
162
163 string hash = CreateHashOfImage(filePaths[i]);
164 imagesHashed++;
165 if (hash != string.Empty)
166 {
167 if (hashesFound.Contains(hash))
168 {
169 DuplicatedImageFinder.AddHash(hash, filePaths[i]);
170 RemoveCheckedFileFromList(filePaths[i], filePaths);
171 i--;
172 }
173 else
174 {
175 hashesFound.Add(hash);
176 }
177 }
178 //int progress = (i * 100) / files.Count - 1;
179 //ConsoleUtility.WriteProgressBar(progress, true);
180
181 //DuplicatedImage image = FindDuplicateImage(files[i], files.GetRange(i + 1, files.Count - (i + 1)));
182 //if (image != null)
183 //{
184 // StoreDuplicatedImages(image);
185
186 //}
187 }
188
189 ConsoleUtility.WriteProgressBar(100, true);
190 Console.WriteLine("\nTotal Images checked : {0}\nCollating Data now...", imagesHashed);
191
192 DuplicatedImageFinder.FindOriginals(filePaths, hashesFound);
193
194 DuplicatedImageFinder.SaveData(DUPLICATED_IMAGES_JSON);
195
196 //ConsoleUtility.WriteProgressBar(100, true);
197 Console.WriteLine();
198
199 }
200 else
201 {
202 Console.WriteLine("There are no files in this directory and it's subdirectories, please try again.\n");
203 }
204
205 }
206 else
207 {
208 Console.WriteLine("Path does not exist, please try again.\n");
209 }
210
211
212 }
213 else if (operation[0] == 'D' || operation[0] == 'd')
214 {
215 Console.WriteLine("\n{0}\n", DuplicatedImageFinder.DisplayData());
216 }
217 else if (operation[0] == 'C' || operation[0] == 'c')
218 {
219 // Needs to be fixed
220 using (StreamWriter sw = File.CreateText(TEST_JSON))
221 {
222 sw.WriteLine();
223 }
224 Console.WriteLine("Duplicate file cleared.\n");
225 }
226 else if (operation[0] == 'E' || operation[0] == 'e')
227 {
228 using (StreamWriter sw = File.CreateText(DEFAULT_TXT_OUTPUT_PATH))
229 {
230 sw.WriteLine(DuplicatedImageFinder.DisplayData());
231 }
232 Console.WriteLine("Duplicate file exported.\n");
233 }
234 else if (operation[0] == 'V' || operation[0] == 'v')
235 {
236 Console.WriteLine("Verify duplicate images found...\n");
237 DuplicatedImageFinder.VerifyDuplicates();
238 DuplicatedImageFinder.SaveData(DUPLICATED_IMAGES_JSON);
239 }
240 else if (operation[0] == 'H' || operation[0] == 'h')
241 {
242 Console.Write("Enter path of file to hash: ");
243 string filePath = Console.ReadLine();
244 if (Path.Exists(filePath))
245 {
246 string hash = CreateHashOfImage(filePath);
247 Console.WriteLine("Hash of image : {0}\n", hash);
248 }
249 else
250 {
251 Console.WriteLine("Path does not exist, please try again.\n");
252 }
253 }
254 else if (operation[0] == 'Q' || operation[0] == 'q')
255 {
256 Console.WriteLine("Thank you, good bye!");
257 return; // Quit's program
258 }
259 else
260 {
261 Console.WriteLine("Invalid input, please try again.\n");
262 }
263 }
264
265
266
267
268 }
269 //string originalFile = "C:\\Users\\Alexa\\repos\\duplicate-file-locator\\test_files\\P1040133.JPG";
270 //string diffNameFile = "C:\\Users\\Alexa\\repos\\duplicate-file-locator\\test_files\\diff_name.JPG";
271 //string sameNameFile = "C:\\Users\\Alexa\\repos\\duplicate-file-locator\\test_files\\same_name\\P1040133.JPG";
272 //string sameNameDiffDateFile = "C:\\Users\\Alexa\\repos\\duplicate-file-locator\\test_files\\same_name_diff_date\\P1040133.JPG";
273
274 //Console.WriteLine("Comparing \n{0} \nwith \n{1}", originalFile, originalFile);
275 //Console.WriteLine("Result: {0}\n", CompareFiles(originalFile, originalFile));
276
277 //Console.WriteLine("Comparing \n{0} \nwith \n{1}", originalFile, diffNameFile);
278 //Console.WriteLine("Result: {0}\n", CompareFiles(originalFile, diffNameFile));
279
280 //Console.WriteLine("Comparing \n{0} \nwith \n{1}", originalFile, sameNameFile);
281 //Console.WriteLine("Result: {0}\n", CompareFiles(originalFile, sameNameFile));
282
283 //Console.WriteLine("Comparing \n{0} \nwith \n{1}", originalFile, sameNameDiffDateFile);
284 //Console.WriteLine("Result: {0}\n", CompareFiles(originalFile, sameNameDiffDateFile));
285
286
287 }
288 }
289}