public static void GetDiffs(List<string> filePaths, System.ComponentModel.BackgroundWorker worker, out TagDiffs diffs, out FileErrors errors) { diffs = new TagDiffs(); errors = new FileErrors(); int index = 0; int total = filePaths.Count(); foreach (var filePath in filePaths) { if (worker.CancellationPending) return; index += 1; worker.ReportProgress(index*100/total, filePath); AddDiffs(diffs, errors, filePath); } }
private static void AddDiffs(TagDiffs diffs, FileErrors errors, string filePath) { TagLib.File file; try { file = TagLib.File.Create(filePath); } catch (TagLib.CorruptFileException ex) { errors.Add(new FileError { FilePath = filePath, Message = ex.Message }); return; } var current = new Dictionary<Tags, string>(); var expected = new Dictionary<Tags, string>(); var folderPath = System.IO.Path.GetDirectoryName(filePath); var parts = folderPath.Split('\\'); expected.Add(Tags.Album, parts[parts.Length - 1]); expected.Add(Tags.Performers, parts[parts.Length - 2]); expected.Add(Tags.AlbumArtists, parts[parts.Length - 2]); expected.Add(Tags.Genre, parts[parts.Length - 3]); expected.Add(Tags.Title, System.IO.Path.GetFileNameWithoutExtension(filePath)); current.Add(Tags.Album, file.Tag.Album); current.Add(Tags.Performers, String.Join(";", file.Tag.Performers)); current.Add(Tags.AlbumArtists, String.Join(";", file.Tag.AlbumArtists)); current.Add(Tags.Genre, String.Join(";", file.Tag.Genres)); current.Add(Tags.Title, file.Tag.Title); foreach (var tag in expected.Keys) { if (expected[tag] != current[tag]) { var diff = new TagDiff(); diff.Tag = tag; diff.FilePath = filePath; diff.OldValue = current[tag]; diff.NewValue = expected[tag]; diffs.Add(diff); } } }
private static void WriteDiff(string filePath, TagDiffs diffs) { var file = TagLib.File.Create(filePath); foreach (var diff in diffs) { switch (diff.Tag) { case Tags.Genre: file.Tag.Genres = new string[] { diff.NewValue }; break; case Tags.Performers: file.Tag.Performers = new string[] { diff.NewValue }; break; case Tags.Album: // if (diff.NewValue != null && diff.NewValue.Length > 30) diff.NewValue = diff.NewValue.Substring(0, 30); file.Tag.Album = diff.NewValue; break; case Tags.Title: file.Tag.Title = diff.NewValue; break; case Tags.AlbumArtists: file.Tag.AlbumArtists = new string[] { diff.NewValue }; break; } } file.Save(); }