files are mp4b
This commit is contained in:
parent
1dd07adcdd
commit
de2df1242b
1 changed files with 14 additions and 8 deletions
22
src/main.rs
22
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<i32> {
|
||||
fn rename_files(directory: &str, base: &str) -> Result<i32> {
|
||||
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<i32> {
|
|||
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<i32> {
|
|||
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)
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue