From 4a34ec0cbdceca51b65164ecbfe76636c2319c83 Mon Sep 17 00:00:00 2001 From: fiso64 Date: Sat, 21 Dec 2024 21:47:06 +0100 Subject: [PATCH] fix move deleting file if same path --- slsk-batchdl/FileManager.cs | 14 ++------------ slsk-batchdl/Program.cs | 2 +- slsk-batchdl/Utilities/Utils.cs | 23 ++++++++++++++++++++++- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/slsk-batchdl/FileManager.cs b/slsk-batchdl/FileManager.cs index 457c250..33e9b58 100644 --- a/slsk-batchdl/FileManager.cs +++ b/slsk-batchdl/FileManager.cs @@ -101,7 +101,7 @@ public class FileManager try { - MoveAndDeleteParent(track.DownloadPath, newFilePath); + Utils.MoveAndDeleteParent(track.DownloadPath, newFilePath, config.parentDir); } catch (Exception ex) { @@ -130,7 +130,7 @@ public class FileManager try { - MoveAndDeleteParent(track.DownloadPath, newFilePath); + Utils.MoveAndDeleteParent(track.DownloadPath, newFilePath, config.parentDir); } catch (Exception ex) { @@ -142,16 +142,6 @@ public class FileManager organized.Add(track); } - - void MoveAndDeleteParent(string oldPath, string newPath) - { - if (Utils.NormalizedPath(oldPath) != Utils.NormalizedPath(newPath)) - { - Directory.CreateDirectory(Path.GetDirectoryName(newPath)); - Utils.Move(oldPath, newPath); - Utils.DeleteAncestorsIfEmpty(Path.GetDirectoryName(oldPath), config.parentDir); - } - } string SubstituteValues(string format, Track track, Soulseek.File? slfile) { diff --git a/slsk-batchdl/Program.cs b/slsk-batchdl/Program.cs index d9f0fef..ab644f7 100644 --- a/slsk-batchdl/Program.cs +++ b/slsk-batchdl/Program.cs @@ -256,7 +256,7 @@ static partial class Program for (int i = 0; i < trackLists.lists.Count; i++) { - if (!enableParallelSearch) Console.WriteLine(); + if (!enableParallelSearch && i > 0) Console.WriteLine(); var tle = trackLists[i]; var config = tle.config; diff --git a/slsk-batchdl/Utilities/Utils.cs b/slsk-batchdl/Utilities/Utils.cs index 5f20329..092e636 100644 --- a/slsk-batchdl/Utilities/Utils.cs +++ b/slsk-batchdl/Utilities/Utils.cs @@ -181,7 +181,13 @@ public static class Utils if (File.Exists(sourceFilePath)) { if (File.Exists(destinationFilePath)) + { + if (Path.GetFullPath(sourceFilePath) == Path.GetFullPath(destinationFilePath)) + { + return; + } File.Delete(destinationFilePath); + } File.Move(sourceFilePath, destinationFilePath); } } @@ -206,6 +212,16 @@ public static class Utils } } + public static void MoveAndDeleteParent(string oldPath, string newPath, string recurseUntil) + { + if (Path.GetFullPath(oldPath) != Path.GetFullPath(newPath)) + { + Directory.CreateDirectory(Path.GetDirectoryName(newPath)); + Move(oldPath, newPath); + DeleteAncestorsIfEmpty(Path.GetDirectoryName(oldPath), recurseUntil); + } + } + public static bool EqualsAny(this string input, string[] values, StringComparison comparison = StringComparison.Ordinal) { foreach (var value in values) @@ -634,7 +650,12 @@ public static class Utils if (string.IsNullOrEmpty(path)) return path; - return path.Replace('\\', '/').TrimEnd('/').Trim(); + path = path.Replace('\\', '/').TrimEnd('/').Trim(); + + while (path.Contains("//")) + path = path.Replace("//", "/"); + + return path; } public static bool IsInDirectory(string path, string dir, bool strict)