From c383b4d3076585931d441ad81299b7bb0449cfd9 Mon Sep 17 00:00:00 2001 From: ByteDream Date: Sun, 4 Dec 2022 19:10:43 +0100 Subject: [PATCH] Fix filename generation if file already exists --- crunchy-cli-core/src/utils/os.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crunchy-cli-core/src/utils/os.rs b/crunchy-cli-core/src/utils/os.rs index 92e4e9f..4919c08 100644 --- a/crunchy-cli-core/src/utils/os.rs +++ b/crunchy-cli-core/src/utils/os.rs @@ -42,7 +42,11 @@ pub fn free_file(mut path: PathBuf) -> PathBuf { i += 1; let ext = path.extension().unwrap().to_string_lossy(); - let filename = path.file_stem().unwrap().to_string_lossy(); + let mut filename = path.file_stem().unwrap().to_str().unwrap(); + + if filename.ends_with(&format!(" ({})", i-1)) { + filename = filename.strip_suffix(&format!(" ({})", i-1)).unwrap(); + } path.set_file_name(format!("{} ({}).{}", filename, i, ext)) }