From de2df1242b66955012668163a3c4bcfca5c6e9dd Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 27 Aug 2023 15:00:26 +0100 Subject: [PATCH] files are mp4b --- src/main.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 08f256c..d112401 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,13 +8,13 @@ use std::{ fn main() { let directory = args().nth(1).unwrap(); println!("Renaming files in {}", directory); - match rename_files(&directory) { + match rename_files(&directory, &directory) { Ok(count) => println!("Renamed {} files", count), Err(e) => eprintln!("Error: {}", e), } } -fn rename_files(directory: &str) -> Result { +fn rename_files(directory: &str, base: &str) -> Result { let entries = read_dir(directory).context("Failed to read directory")?; let mut count = 0; for entry in entries { @@ -22,7 +22,7 @@ fn rename_files(directory: &str) -> Result { let path = entry.path(); if path.is_dir() { if let Some(sub_dir) = path.to_str() { - count += rename_files(sub_dir).context("Failed to rename files")?; + count += rename_files(sub_dir, base).context("Failed to rename files")?; } } if path.is_file() { @@ -34,15 +34,21 @@ fn rename_files(directory: &str) -> Result { count += 1; let new_name = match album.len() { 0 => format!("{artist}/{title}.mp4a"), - _ => format!("{artist}/{album}/{title}.mp4a"), + _ => format!("{artist}/{album}/{title}.mp4b"), }; println!("Renaming {} to {}", path.display(), new_name); - let target_path = Path::new(&new_name); + let target_path = Path::new(&base).join(new_name); if !target_path.exists() { - create_dir_all(&path.parent().unwrap()) - .context("Failed to create directory")?; + let dir = target_path.parent().with_context(|| { + format!("Failed to get parent: {:#?}", target_path) + })?; + create_dir_all(&dir).with_context(|| { + format!("Failed to create directory: {:#?}", dir) + })?; } - rename(&path, &target_path).context("Failed to rename file")?; + rename(&path, &target_path).with_context(|| { + format!("Failed to rename file: {:#?}", target_path) + })?; } } }