private string EditBaseInfoTag(File file, string path) { var taglibFile = file; var previous = _hashSet.First(x => taglibFile != null && x.Key.Equals(path)); var baseInfoTag = new BaseInfoTag(); if (previous.Value != null) { var tag = taglibFile.TagTypes != TagTypes.Id3v2 ? taglibFile.Tag : taglibFile.GetTag(TagTypes.Id3v2); baseInfoTag = new BaseInfoTag(tag.JoinedPerformers, tag.FirstPerformer, tag.Album, tag.Title, taglibFile.Name); BaseInfoTag crap; _hashSet.TryRemove(previous.Key, out crap); _hashSet.TryAdd(baseInfoTag.FileInfo, baseInfoTag); } return baseInfoTag.FileInfo; }
/// <summary> /// Moves the file and renames it to the new structure /// </summary> /// <param name="baseInfoTag">Metadatas of the file</param> /// <param name="basePath">Path of the "music" folder</param> /// <returns>Success or not</returns> public static bool MoveFile(BaseInfoTag baseInfoTag, string basePath) { try { var path = $"{basePath}\\{baseInfoTag.NewBasePath}"; Directory.CreateDirectory(path); var oldFileInfo = new FileInfo(baseInfoTag.FileInfo); // falls die Datei schon existiert var counter = 1; var newFileInfo = new FileInfo(Path.Combine(path, oldFileInfo.Name)); var nameWithoutExtension = Path.GetFileNameWithoutExtension(oldFileInfo.Name); while (newFileInfo.Exists) { var tempFileName = $"{nameWithoutExtension} ({counter++})"; newFileInfo = new FileInfo(Path.Combine(path, $"{tempFileName}{oldFileInfo.Extension}")); } // Datei in neue Struktur kopieren Logger.Info( $"Moving \"{oldFileInfo.FullName}\"\r\n to \"{newFileInfo.FullName}\""); oldFileInfo.MoveTo(newFileInfo.FullName); return true; } catch (Exception ex) { Logger.Error($"{ex.Message} -> \"{baseInfoTag.FileInfo}\"", ex); return false; } }
private async void RenameFiles() { if (_hashSet.Any()) { StartOrStop(true); _logger.Info( $"Renaming {_hashSet.Count} files ({string.Join("; ", _extensions)}) to \"Joined Performers - Titel\""); var already = 0; var successfully = 0; var withException = 0; var actionBlock = new ActionBlock<BaseInfoTag>(b => { BeginInvoke((MethodInvoker)PerformStep); var result = Helper.RenameFile(b); Actiontype actiontypeResult; if (Enum.TryParse(result, out actiontypeResult)) { switch (actiontypeResult) { case Actiontype.Already: already++; break; case Actiontype.Exception: withException++; break; } } else { var previous = _hashSet.First(x => x.Key.Equals(b.FileInfo)); if (previous.Value != null) { var baseInfoTag = new BaseInfoTag(b.JoinedPerformers, b.FirstPerformer, b.Album, b.Title, result); BaseInfoTag crap; _hashSet.TryRemove(previous.Key, out crap); _hashSet.TryAdd(baseInfoTag.FileInfo, baseInfoTag); successfully++; } } }, new ExecutionDataflowBlockOptions { TaskScheduler = TaskScheduler.Default, MaxDegreeOfParallelism = _maxDegreeOfParallelism }); await ExecuteActionBlock(actionBlock, withException); if (already > 0) _logger.Info($"{already} files have already a fitting name and are ignored"); var percentage = Helper.CalculatePercentage(successfully, _hashSet.Count); _logger.Info( $"{successfully} files were renamed within {_stopwatch.Elapsed} ({percentage}%)"); BindAndSort(); } }
/// <summary> /// Renames the file to the new structure /// </summary> /// <param name="baseInfoTag">Metadatas of the file</param> /// <returns>The new full path of the file</returns> public static string RenameFile(BaseInfoTag baseInfoTag) { try { if (!string.IsNullOrWhiteSpace(baseInfoTag.JoinedPerformers) && !string.IsNullOrWhiteSpace(baseInfoTag.Title)) { var oldFileInfo = new FileInfo(baseInfoTag.FileInfo); var path = oldFileInfo.DirectoryName; if (path != null) { var newFileName = $"{baseInfoTag.JoinedPerformers} - {baseInfoTag.Title}"; newFileName = newFileName.RemoveInvalidPathCharsAndToTitleCase().Trim(); var newFileInfo = new FileInfo(Path.Combine(path, $"{newFileName}{oldFileInfo.Extension.ToLower()}")); if (!newFileInfo.FullName.ToLower().Equals(oldFileInfo.FullName.ToLower())) { var counter = 1; var isTheSame = false; while (newFileInfo.Exists) { isTheSame = newFileInfo.FullName.ToLower().Equals(oldFileInfo.FullName.ToLower()); if (isTheSame) break; var tempFileName = $"{newFileName} ({counter++})"; newFileInfo = new FileInfo(Path.Combine(path, $"{tempFileName}{oldFileInfo.Extension.ToLower()}")); } if (!isTheSame) { // Datei umbenennen Logger.Info( $"Renaming \"{oldFileInfo.FullName}\"\r\n to \"{newFileInfo.FullName}\""); File.Move(baseInfoTag.FileInfo, newFileInfo.FullName); return newFileInfo.FullName; } } } } return Actiontype.Already.ToString(); } catch (Exception ex) { Logger.Error($"{ex.Message} -> \"{baseInfoTag.FileInfo}\"", ex); return Actiontype.Exception.ToString(); } }