rename files

This commit is contained in:
Paul Campbell 2023-08-27 14:28:41 +01:00
parent aa3e655520
commit 1dd07adcdd
3 changed files with 25 additions and 16 deletions

7
Cargo.lock generated
View file

@ -2,10 +2,17 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "anyhow"
version = "1.0.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6"
[[package]] [[package]]
name = "auto-file-mp4" name = "auto-file-mp4"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"mp4", "mp4",
"taglib", "taglib",
] ]

View file

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
mp4 = "0.14.0" mp4 = "0.14.0"
taglib = "1.0.0" taglib = "1.0.0"
anyhow = "1.0.75"

View file

@ -1,7 +1,12 @@
use std::fs::{self}; use anyhow::{Context, Result};
use std::{
env::args,
fs::{create_dir_all, read_dir, rename},
path::Path,
};
fn main() { fn main() {
let directory = std::env::args().nth(1).unwrap(); let directory = args().nth(1).unwrap();
println!("Renaming files in {}", directory); println!("Renaming files in {}", directory);
match rename_files(&directory) { match rename_files(&directory) {
Ok(count) => println!("Renamed {} files", count), Ok(count) => println!("Renamed {} files", count),
@ -9,25 +14,17 @@ fn main() {
} }
} }
fn rename_files(directory: &str) -> Result<i32, Box<dyn std::error::Error>> { fn rename_files(directory: &str) -> Result<i32> {
// Read the directory entries let entries = read_dir(directory).context("Failed to read directory")?;
let entries = fs::read_dir(directory)?;
let mut count = 0; let mut count = 0;
// Iterate over each entry
for entry in entries { for entry in entries {
let entry = entry?; let entry = entry.context("Failed to read entry")?;
// Get the file path
let path = entry.path(); let path = entry.path();
if path.is_dir() { if path.is_dir() {
if let Some(sub_dir) = path.to_str() { if let Some(sub_dir) = path.to_str() {
count += rename_files(sub_dir)?; count += rename_files(sub_dir).context("Failed to rename files")?;
} }
} }
// Check if the entry is a file
if path.is_file() { if path.is_file() {
if let Ok(file) = taglib::File::new(&path) { if let Ok(file) = taglib::File::new(&path) {
if let Ok(tag) = file.tag() { if let Ok(tag) = file.tag() {
@ -35,19 +32,23 @@ fn rename_files(directory: &str) -> Result<i32, Box<dyn std::error::Error>> {
if let Some(artist) = tag.artist() { if let Some(artist) = tag.artist() {
let album = parse_album(&title); let album = parse_album(&title);
count += 1; count += 1;
// println!("{album}: {title} by {artist}");
let new_name = match album.len() { let new_name = match album.len() {
0 => format!("{artist}/{title}.mp4a"), 0 => format!("{artist}/{title}.mp4a"),
_ => format!("{artist}/{album}/{title}.mp4a"), _ => format!("{artist}/{album}/{title}.mp4a"),
}; };
println!("Renaming {} to {}", path.display(), new_name); println!("Renaming {} to {}", path.display(), new_name);
let target_path = Path::new(&new_name);
if !target_path.exists() {
create_dir_all(&path.parent().unwrap())
.context("Failed to create directory")?;
}
rename(&path, &target_path).context("Failed to rename file")?;
} }
} }
} }
} }
} }
} }
Ok(count) Ok(count)
} }