public void FindDuplicateFiles_GoldFlow() { string fileHash = "bbaaddbbeeeeff"; long filesize = 626; string filename = "BOOTNXT"; int expectedNumberOfResults = 1; File expectedDuplicateFile = new File() { Filename = filename, SizeInKiloBytes = filesize, FullPath = $@"C:\{filename}", Hash = fileHash, }; string[] mockedFiles = new[] { $@"C:\{filename}" }; Mock <IFileHelpers> fileHelperMock = new Moq.Mock <IFileHelpers>(); fileHelperMock.Setup(a => a.GetFileName(It.IsAny <string>())).Returns(filename); fileHelperMock.Setup(a => a.GetFileSize(It.IsAny <string>())).Returns(filesize); fileHelperMock.Setup(f => f.WalkFilePaths(It.IsAny <FindDuplicatesJob>())).Returns(mockedFiles); fileHelperMock.Setup(g => g.GetHashedValue(It.IsAny <string>(), It.IsAny <long>(), It.IsAny <long>())).Returns(fileHash); DuplicateFinder sut = new DuplicateFinder(fileHelperMock.Object); ConcurrentDictionary <string, List <File> > results = sut.FindDuplicateFiles(_testJob); Assert.NotNull(results); Assert.AreEqual(expectedNumberOfResults, results.Count); Assert.AreEqual(results.FirstOrDefault().Value[0], expectedDuplicateFile); }
private void PopulatedExpectedDuplicateFiles(string filename, long filesize, string fileHash) { File tempFile = new File() { Filename = filename, SizeInKiloBytes = filesize, FullPath = $@"C:\{filename}", Hash = fileHash, }; expectedDuplicateFiles.AddOrUpdate(fileHash, new List <File>() { tempFile }, (key, value) => { value.Add(tempFile); return(value); }); }
internal ConcurrentDictionary <string, List <File> > PopulateFileMetaData(string[] files, ConcurrentDictionary <string, List <File> > duplicateDictionary) { Console.WriteLine("Populating metadata for discovered files..."); bool isInConsole = IsConsoleApplication(); var pb = isInConsole ? new ProgressBar(PbStyle.DoubleLine, files.Length) : null; int lastIncrement = 0; if (isInConsole) { pb.Refresh(0, "Initializing..."); } int i = 0; foreach (string filePath in files) { // Only update the progress bar occasionally if (isInConsole && Math.Floor(i * 100.0 / files.Length) > lastIncrement || i == files.Length) { string tempFilePath = filePath; if (filePath.Contains("{")) { tempFilePath = tempFilePath.Replace("{", ""); } if (filePath.Contains("}")) { tempFilePath = tempFilePath.Replace("}", ""); } try { pb.Refresh(i, tempFilePath); lastIncrement = (i * 100 / files.Length); } catch (Exception e) { Console.WriteLine(e); } } if (!String.IsNullOrEmpty(filePath)) { long fileSize = _fileSystemHelper.GetFileSize(filePath); File tempFile = new File { FullPath = filePath, Filename = _fileSystemHelper.GetFileName(filePath), SizeInKiloBytes = fileSize, //todo: add option to hash only a portion of the file AND / OR check the files table. if the filename && size && path are the same as an entry in the files table, don't bother hashing (optionally) - just use the value from the table Hash = _fileSystemHelper.GetHashedValue(filePath, fileSize, hashLimit) }; //Ignore empty directory placeholder if (tempFile.Filename == "_._") { continue; } duplicateDictionary.AddOrUpdate(tempFile.Hash, new List <File>() { tempFile }, (key, value) => { value.Add(tempFile); return(value); }); } i++; } Console.WriteLine("\n...done."); return(duplicateDictionary); }