private void ReadFiles(string basePath, List <string> files, string target) { Dictionary <string, MusicFileInfo> infos = null; MusicFileInfo mfi = null; string key = null; string duplicateFile = null; if (string.IsNullOrEmpty(basePath)) { basePath = Helper.getCommonPrefix(files); } if (basePath.EndsWith("\\")) { basePath = basePath.Substring(0, basePath.Length - 1); } if (target == "left") { infosLeft = new Dictionary <string, MusicFileInfo>(); infos = infosLeft; } else { infosRight = new Dictionary <string, MusicFileInfo>(); infos = infosRight; } foreach (string file in files) { duplicateFile = null; if (IsMusicFile(file)) { mfi = new MusicFileInfo(basePath, file, true); key = mfi.Key; if (infos.ContainsKey(key)) { duplicateFile = infos[key].CompletePath; while (infos.ContainsKey(key)) { key = mfi.ModifyKey(); } mfi.DuplicateFile = duplicateFile; } infos.Add(key, mfi); } } }
private void compareLists(Dictionary <string, MusicFileInfo> info1, Dictionary <string, MusicFileInfo> info2, string typeLeft, string typeRight) { MusicFileInfo fi1 = null; MusicFileInfo fi2 = null; StringBuilder text = null; ComparisonInfo cp = null; ComparisonInfo cpt = null; string[] pathSubstitutions = null; if (txt_pathSubst.Text.Trim().Length > 0) { pathSubstitutions = txt_pathSubst.Text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None); } Dictionary <string, object[]> results = null; object[] result = null; List <string> foundRight = new List <string>(); cpInfos = new Dictionary <string, ComparisonInfo>(); bool found = false; int minBitrate = 0; if (!string.IsNullOrEmpty(txt_minBitrate.Text)) { minBitrate = Convert.ToInt32(txt_minBitrate.Text); } if (info1 != null && info1.Count > 0 && info2 != null && info2.Count > 0) { // Links-rechts Vergleich foreach (KeyValuePair <string, MusicFileInfo> kvp in info1) { found = false; fi1 = kvp.Value; if (info2.ContainsKey(fi1.Key)) { fi2 = info2[fi1.Key]; cp = new ComparisonInfo(fi1, fi2, minBitrate); cpInfos.Add(fi1.Key, cp); foundRight.Add(fi1.Key); found = true; } else { cp = null; foreach (MusicFileInfo comparer in info2.Values) { cpt = new ComparisonInfo(fi1, comparer, minBitrate); if (cpt.Equality >= 50) { if (cp == null || cp.Equality < cpt.Equality) { cp = cpt; } } } if (cp != null && cp.Equality >= 50) { cpInfos.Add(fi1.Key, cp); foundRight.Add(kvp.Key); found = true; } } if (!found) { cp = new ComparisonInfo(fi1, null, minBitrate); cpInfos.Add(kvp.Key, cp); } } // Rechts-Links Vergleich fuer noch nicht gefundene Matches if (foundRight.Count < info1.Count && typeLeft == "directory") { foreach (KeyValuePair <string, MusicFileInfo> kvp in info2) { fi2 = kvp.Value; found = false; if (!foundRight.Contains(fi2.Key)) { cp = null; foreach (MusicFileInfo comparer in info1.Values) { cpt = new ComparisonInfo(fi2, comparer, minBitrate); if (cpt.Equality >= 50) { if (cp == null || cp.Equality < cpt.Equality) { cp = cpt; } } } if (cp != null && cp.Equality >= 50) { cpInfos.Add(fi2.Key, cp); foundRight.Add(kvp.Key); found = true; } if (!found) { cp = new ComparisonInfo(null, fi2, minBitrate); cpInfos.Add(fi2.Key, cp); } } } } } if (typeLeft == "playlist") { results = new Dictionary <string, object[]>(); foreach (KeyValuePair <string, ComparisonInfo> kvp in cpInfos) { fi1 = kvp.Value.FileLeft; fi2 = kvp.Value.FileRight; if (fi1 != null) { result = new object[15]; result[0] = (object)false; result[1] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Artist) ? "" : fi1.Artist)); result[2] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Title) ? "" : fi1.Title)); result[3] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Path) ? "" : fi1.Path)); result[4] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Extension) ? "" : fi1.Extension)); result[5] = (object)(fi1 == null ? 0 : fi1.Length); result[6] = (object)(fi1 == null ? 0 : fi1.Bitrate); result[7] = (object)("" + kvp.Value.Equality); result[8] = (object)kvp.Value.ShouldBeTranscoded; result[9] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Artist) ? "" : fi2.Artist)); result[10] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Title) ? "" : fi2.Title)); result[11] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Path) ? "" : fi2.Path)); result[12] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Extension) ? "" : fi2.Extension)); result[13] = (object)(fi2 == null ? 0 : fi2.Length); result[14] = (object)(fi2 == null ? 0 : fi2.Bitrate); results.Add(kvp.Key, result); } } bool first = true; txt_playlistOutput.Text = ""; text = new StringBuilder(); foreach (KeyValuePair <string, ComparisonInfo> kvp in cpInfos.Where(x => x.Value.FileRight != null)) { if (first) { first = false; } else { text.Append(System.Environment.NewLine); } string completePath = kvp.Value.FileRight.CompletePath; if (pathSubstitutions != null && pathSubstitutions.Length > 0) { for (int i = 0; i < pathSubstitutions.Length; i = i + 2) { completePath = Regex.Replace(completePath, @pathSubstitutions[i], @pathSubstitutions[i + 1], RegexOptions.IgnoreCase); //completePath = Regex.Replace(completePath, @".*handy\\", "../", RegexOptions.IgnoreCase); } } if (!string.IsNullOrEmpty(txt_delimiter.Text)) { completePath = completePath.Replace("\\", txt_delimiter.Text); } text.Append(completePath); } txt_playlistOutput.Text = text.ToString(); } else if (typeLeft == "directories" && typeRight == "directories") { results = new Dictionary <string, object[]>(); foreach (KeyValuePair <string, ComparisonInfo> kvp in cpInfos) { result = new object[15]; fi1 = kvp.Value.FileLeft; fi2 = kvp.Value.FileRight; result[0] = (object)false; result[1] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Artist) ? "" : fi1.Artist)); result[2] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Title) ? "" : fi1.Title)); result[3] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Path) ? "" : fi1.Path)); result[4] = (object)(fi1 == null ? "not found" : (string.IsNullOrEmpty(fi1.Extension) ? "" : fi1.Extension)); result[5] = (object)(fi1 == null ? 0 : fi1.Length); result[6] = (object)(fi1 == null ? 0 : fi1.Bitrate); result[7] = (object)("" + kvp.Value.Equality); result[8] = (object)kvp.Value.ShouldBeTranscoded; result[9] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Artist) ? "" : fi2.Artist)); result[10] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Title) ? "" : fi2.Title)); result[11] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Path) ? "" : fi2.Path)); result[12] = (object)(fi2 == null ? "not found" : (string.IsNullOrEmpty(fi2.Extension) ? "" : fi2.Extension)); result[13] = (object)(fi2 == null ? 0 : fi2.Length); result[14] = (object)(fi2 == null ? 0 : fi2.Bitrate); results.Add(kvp.Key, result); } } DataGridViewRow dgvr = null; dgv_directories.SuspendLayout(); dgv_directories.Rows.Clear(); if (results != null && results.Count > 0) { foreach (KeyValuePair <string, object[]> kvp in results) { dgvr = (DataGridViewRow)dgv_directories.RowTemplate.Clone(); dgvr.CreateCells(dgv_directories, kvp.Value); dgvr.Tag = cpInfos[kvp.Key]; dgv_directories.Rows.Add(dgvr); } } dgv_directories.ResumeLayout(); dgv_directories.PerformLayout(); }
public ComparisonInfo(MusicFileInfo fileLeft, MusicFileInfo fileRight, int minBitrate) { this.f1 = fileLeft; this.f2 = fileRight; if (f1 != null && f2 != null) { if (f1.Modified > f2.Modified) { newer = "left"; } else if (f1.Modified == f2.Modified) { newer = "same"; } else { newer = "rigth"; } if (f1.Length > f2.Length) { longer = "left"; } else if (f1.Length == f2.Length) { longer = "same"; } else { longer = "right"; } if ((f1.Lossless && f2.Lossless)) { quality = "lossless"; } else if (f1.Lossless && !f2.Lossless) { quality = "left"; } else if (!f1.Lossless && f2.Lossless) { quality = "right"; } else if (f1.Extension.Equals(f2.Extension, StringComparison.InvariantCultureIgnoreCase) && f1.Length == f2.Length) { quality = "same"; } else if (f1.Length >= f2.Length) { quality = "left"; } else { quality = "right"; } if (minBitrate > 0) { if (f1.Lossless && !f2.Lossless && f2.Bitrate > 0 && f2.Bitrate < minBitrate) { shouldBeTranscoded = "right"; } else if (!f1.Lossless && f2.Lossless && f1.Bitrate > 0 && f1.Bitrate < minBitrate) { shouldBeTranscoded = "left"; } } if (f1.FileName.ToLower() == f2.FileName.ToLower() && f1.Extension.ToLower() == f2.Extension.ToLower() && f1.Length == f2.Length) { equality = 100; } else { if (!string.IsNullOrEmpty(f1.Artist) && !string.IsNullOrEmpty(f2.Artist) && !string.IsNullOrEmpty(f1.Title) && !string.IsNullOrEmpty(f2.Title) && f1.Artist.Equals(f2.Artist, StringComparison.InvariantCultureIgnoreCase) && f1.Title.Equals(f2.Title, StringComparison.InvariantCultureIgnoreCase)) { equality = 85; if (f1.Extension.Equals(f2.Extension, StringComparison.InvariantCultureIgnoreCase)) { equality += 5; } if (f1.TagsRead && f2.TagsRead && !f1.TagsGuessed && !f2.TagsGuessed) { equality += 5; } } else if (!string.IsNullOrEmpty(f1.Title) && !string.IsNullOrEmpty(f2.Title) && f1.Title.Equals(f2.Title, StringComparison.InvariantCultureIgnoreCase)) { equality = 50; if (f1.Extension.Equals(f2.Extension, StringComparison.InvariantCultureIgnoreCase)) { equality += 5; } if (f1.TagsRead && f2.TagsRead && !f1.TagsGuessed && !f2.TagsGuessed) { equality += 5; } } } } }