Example #1
0
        public ScanResult FindDuplicateFiles(String dirPath)
        {
            worker.ReportProgress(0, "Preparing files ...");
            FileInfo[] files = new DirectoryInfo(dirPath).GetFiles("*", SearchOption.AllDirectories);
            Array.Sort(files, Comparer<FileInfo>.Create((a,b) => b.FullName.CompareTo(a.FullName)));
            
            int total = files.Length;
            double progress = 0;
            IDictionary<long, IDictionary<FileInfo, IList<FileInfo>>> byteTable = new Dictionary<long, IDictionary<FileInfo, IList<FileInfo>>>();
            foreach (FileInfo file in files) {
                worker.ReportProgress((int)(++progress/total*100), String.Format("Scanning files... ({0}/{1})", progress, total));

                // Compare size
                long fileSize = file.Length;
                if (!byteTable.ContainsKey(fileSize))
                    byteTable.Add(fileSize, new Dictionary<FileInfo, IList<FileInfo>>());
                
                // Compare contents of the files with the same size
                IDictionary<FileInfo, IList<FileInfo>> fileTable = byteTable[fileSize]; // All files in fileMap have the same size
                bool foundDuplicate = false;
                // Compare the current file to each file in fileTable
                foreach (KeyValuePair<FileInfo, IList<FileInfo>> pair in fileTable) {
                    // If find a duplicate, add the file to the duplicate-files-list and break the iteration
                    if (FilesAreEqual(pair.Key, file)) {
                        foundDuplicate = true;
                        pair.Value.Add(file);
                        break;
                    }
                }
                // No duplicate found, create a new entry in fileTable
                if (!foundDuplicate)
                    fileTable.Add(file, new List<FileInfo>());
            }

            // Build the result
            worker.ReportProgress(100, "Build the result ...");
            ScanResult result = new ScanResult();
            int sum = 0;
            foreach (IDictionary<FileInfo, IList<FileInfo>> fileTable in byteTable.Values) {
                foreach (KeyValuePair<FileInfo, IList<FileInfo>> pair in fileTable) {
                    if (pair.Value.Count > 0) {
                        ISet<FileInfo> list = new SortedSet<FileInfo>(Comparer<FileInfo>.Create((a, b) => b.FullName.CompareTo(a.FullName)));
                        list.Add(pair.Key);
                        result.RemoveList.Add(pair.Key, false);
                        foreach (FileInfo file in pair.Value) { 
                            list.Add(file);
                            result.RemoveList.Add(file, true);
                        }
                        result.FileList.Add(pair.Key, list);
                        sum += pair.Value.Count;
                    }
                }
            }
            result.NumDuplicates = sum;
            return result;
        }
Example #2
0
        private void button_scan_Click(object sender, EventArgs e)
        {
            button_scan.Enabled = false;
            listBox_details.DrawItem -= drawItemEventHandler;
            duplicates = null;

            listBox_overview.Items.Clear();
            listBox_details.Items.Clear();
            textBox_filePath.Clear();
            toolStripProgressBar1.Value = 0;
            toolStripStatusLabel1.Text = "";
            
            backgroundWorker_scan.RunWorkerAsync();
        }
Example #3
0
 private void backgroundWorker_scan_DoWork(object sender, DoWorkEventArgs e)
 {
     duplicates = new DuplicateFinder(sender as BackgroundWorker).FindDuplicateFiles(textBox_folder.Text);
 }