private void AddSliToListView(SLItem sli, ListView lv, bool fChecked) { ListViewItem lvi = new ListViewItem(); lvi.SubItems.Add(new ListViewItem.ListViewSubItem()); lvi.SubItems.Add(new ListViewItem.ListViewSubItem()); lvi.SubItems.Add(new ListViewItem.ListViewSubItem()); lvi.Tag = sli; lvi.SubItems[2].Text = sli.m_sPath; lvi.SubItems[1].Text = sli.m_lSize.ToString("###,###,###"); lvi.SubItems[0].Text = sli.m_sName; if (fChecked) lvi.Checked = true; lv.Items.Add(lvi); }
/* E H _ F I N D D U P L I C A T E S */ /*---------------------------------------------------------------------------- %%Function: EH_FindDuplicates %%Qualified: SList.MainForm.EH_FindDuplicates %%Contact: rlittle ----------------------------------------------------------------------------*/ private void EH_FindDuplicates(object sender, System.EventArgs e) { SLItem []rgsli = new SLItem[m_lv.Items.Count]; int start, end, sum = 0; int min = 999999, max = 0, c = 0; start = Environment.TickCount; int i, iMac; for (i = 0, iMac = m_lv.Items.Count; i < iMac; i++) { rgsli[i] = (SLItem)m_lv.Items[i].Tag; rgsli[i].ClearDupeChain(); rgsli[i].m_fMarked = false; } Array.Sort(rgsli, new SLItemComparer(SLItem.SLItemCompare.CompareSize)); m_lv.BeginUpdate(); m_lv.Items.Clear(); i = 0; iMac = rgsli.Length; int iIncrement = Math.Max(1, iMac / 1000); int iLast = 0; Cursor crsSav = this.Cursor; // start a wait cursor this.Cursor = Cursors.WaitCursor; m_prbarOverall.Show(); for ( ; i < iMac; i++) { int iDupe, iDupeMac; if (iLast + iIncrement < i) { m_prbarOverall.Value = Math.Min(1000, (int)(i / iIncrement)); iLast = m_prbarOverall.Value * iIncrement; } if (rgsli[i].m_fMarked) continue; // search forward for dupes for (iDupe = i + 1, iDupeMac = rgsli.Length; iDupe < iDupeMac; iDupe++) { if (rgsli[iDupe].m_fMarked == true) continue; if (rgsli[i].m_lSize == rgsli[iDupe].m_lSize) { // do more extensive check here...for now, the size and the name is enough if (m_cbCompareFiles.Checked) { c++; if (FCompareFiles(rgsli[i], rgsli[iDupe], ref min, ref max, ref sum)) { if (rgsli[i].m_fMarked == false) AddSliToListView(rgsli[i], m_lv, true); if (rgsli[iDupe].m_fMarked == false) AddSliToListView(rgsli[iDupe], m_lv); rgsli[i].m_fMarked = rgsli[iDupe].m_fMarked = true; rgsli[i].AddDupeToChain(rgsli[iDupe]); } } else { if (rgsli[i].m_sName == rgsli[iDupe].m_sName) { if (rgsli[i].m_fMarked == false) AddSliToListView(rgsli[i], m_lv); if (rgsli[iDupe].m_fMarked == false) AddSliToListView(rgsli[iDupe], m_lv); rgsli[i].m_fMarked = rgsli[iDupe].m_fMarked = true; rgsli[i].AddDupeToChain(rgsli[iDupe]); } } } else break; // no reason to continue if the lengths changed; we sorted by length } } m_prbar.Hide(); m_prbarOverall.Hide(); if (m_cbCompareFiles.Checked) m_stbpMainStatus.Text = "Search complete. Duplicates filtered by file compare."; else m_stbpMainStatus.Text = "Search complete. Duplicates filtered by size and name."; m_lv.EndUpdate(); this.Cursor = crsSav; end = Environment.TickCount; int len = end - start; if (c == 0) c = 1; int avg = len / c; int avg2 = sum / c; m_stbpSearch.Text = len.ToString() + "ms, (" + min.ToString() + ", " + max.ToString() + ", " + avg.ToString() + ", " + avg2.ToString() + ", " + c.ToString() + ")"; }
private void AddDirectory(DirectoryInfo di, ListView lv, string sPattern, bool fRecurse) { FileInfo []rgfi; try { rgfi = di.GetFiles(sPattern); } catch { return; } int i, iMac; for (i = 0, iMac = rgfi.Length; i < iMac; i++) { SLItem sli = new SLItem(rgfi[i].Name, rgfi[i].Length, rgfi[i].DirectoryName, rgfi[i]); Application.DoEvents(); AddSliToListView(sli, lv); } if (fRecurse) { DirectoryInfo []rgdi; rgdi = di.GetDirectories(); if (rgdi != null) { for (i = 0, iMac = rgdi.Length; i < iMac; i++) { AddDirectory(rgdi[i], lv, sPattern, fRecurse); } } } }
private void AddSliToListView(SLItem sli, ListView lv) { AddSliToListView(sli, lv, false); }
/* S L I T E M C O M P A R E R */ /*---------------------------------------------------------------------------- %%Function: SLItemComparer %%Qualified: SList.SLItemComparer.SLItemComparer %%Contact: rlittle ----------------------------------------------------------------------------*/ public SLItemComparer(SLItem.SLItemCompare slic) { m_slic = slic; }
public void UpdateForPrefPath(SLItem sliMaster, string s, bool fMark) { SLItem sli; sliMaster.m_fMarked = fMark; UpdateMark(sliMaster); sli = sliMaster; while ((sli = sli.Prev) != null) { if (sli.MatchesPrefPath(s)) sli.m_fMarked = fMark; else sli.m_fMarked = !fMark; UpdateMark(sli); } sli = sliMaster; while ((sli = sli.Next) != null) { if (sli.MatchesPrefPath(s)) sli.m_fMarked = fMark; else sli.m_fMarked = !fMark; UpdateMark(sli); } }
public void AddDupeToChain(SLItem sli) { if (sli.m_sliNext != null || sli.m_sliPrev != null) throw new Exception("Can't add an sli that already has dupes!"); SLItem sliLast = this; while (sliLast.m_sliNext != null) sliLast = sliLast.m_sliNext; sliLast.m_sliNext = sli; sli.m_sliPrev = sliLast; }
public void ClearDupeChain() { m_sliNext = m_sliPrev = null; }
public static int Compare(SLItem sli1, SLItem sli2, SLItemCompare slic, bool fReverse) { int n = 0; switch (slic) { case SLItemCompare.CompareName: n = String.Compare(sli1.m_sName, sli2.m_sName); if (n == 0) { // they are again equivalent; the difference is now file size n = (int)(sli1.m_lSize - sli2.m_lSize); if (n == 0) { // yeesh. diff is now folder n = String.Compare(sli1.m_sPath, sli2.m_sPath); } } break; case SLItemCompare.CompareSize: n = (int)(sli1.m_lSize - sli2.m_lSize); if (n == 0) { // they are the same; now look at the name n = String.Compare(sli1.m_sName, sli2.m_sName); if (n == 0) { // yeesh. diff is now folder n = String.Compare(sli1.m_sPath, sli2.m_sPath); } } break; case SLItemCompare.ComparePath: n = String.Compare(sli1.m_sPath, sli2.m_sPath); if (n == 0) { // they are equivalent; the difference is now based on the name n = String.Compare(sli1.m_sName, sli2.m_sName); if (n == 0) { // they are again equivalent; the difference is now file size n = (int)(sli1.m_lSize - sli2.m_lSize); } } break; } if (fReverse) return -n; else return n; }
void UpdateMark(SLItem sli) { ListViewItem lvi = LviFromSli(sli); lvi.Checked = sli.m_fMarked; }
void Select(SLItem sli) { if (sli == null) { SystemSounds.Beep.Play(); return; } ListViewItem lvi = LviFromSli(sli); if (lvi != null) { lvi.Selected = true; m_lv.Select(); return; } SystemSounds.Beep.Play(); }
ListViewItem LviFromSli(SLItem sli) { foreach(ListViewItem lvi in m_lv.Items) { if (lvi.Tag == sli) return lvi; } return null; }
private bool FCompareFiles(SLItem sli1, SLItem sli2, ref int min, ref int max, ref int sum) { int nStart = Environment.TickCount; int nEnd; FileStream bs1 = new FileStream(Path.Combine(sli1.m_sPath, sli1.m_sName), FileMode.Open, FileAccess.Read, FileShare.Read, 8, false); FileStream bs2 = new FileStream(Path.Combine(sli2.m_sPath, sli2.m_sName), FileMode.Open, FileAccess.Read, FileShare.Read, 8, false); int lcb = 16; long icb = 0; int i; int iProgress = 0; int iCurProgress = 0; bool fProgress = true; int iIncrement = (int)sli1.m_lSize / 100; long lProgressLast = 0; if (iIncrement == 0) iIncrement = 1; if (sli1.m_lSize < 10000) fProgress = false; if (icb + lcb >= sli1.m_lSize) lcb = (int)(sli1.m_lSize - icb); m_stbpMainStatus.Text = sli1.m_sName; if (fProgress) { m_prbar.Value = iProgress; m_prbar.Show(); } while (lcb > 0) { Application.DoEvents(); if (fProgress) { if (lProgressLast + iIncrement < icb) { iCurProgress = (int)(icb / iIncrement); m_prbar.Value = Math.Min(iCurProgress, 100); iProgress = iCurProgress; lProgressLast = iIncrement * iProgress; } } bs1.Read(m_rgb1, 0, lcb); bs2.Read(m_rgb2, 0, lcb); icb += lcb; i = 0; while (i < lcb) { if (m_rgb1[i] != m_rgb2[i]) { // br1.Close(); // br2.Close(); bs1.Close(); bs2.Close(); m_prbar.Value = 100; nEnd = Environment.TickCount; if ((nEnd - nStart) < min) min = nEnd - nStart; if ((nEnd - nStart) > max) max = (nEnd - nStart); sum += (nEnd - nStart); return false; } i++; } if (lcb < lcbMax) { if ((int)(sli1.m_lSize - icb - 1) == 0) break; lcb *= 2; if (lcb > lcbMax) lcb = lcbMax; } if (icb + lcb >= sli1.m_lSize) lcb = (int)(sli1.m_lSize - icb - 1); } // br1.Close(); // br2.Close(); bs1.Close(); bs2.Close(); m_prbar.Value = 100; nEnd = Environment.TickCount; if ((nEnd - nStart) < min) min = nEnd - nStart; if ((nEnd - nStart) > max) max = (nEnd - nStart); sum += (nEnd - nStart); return true; }