feat: move files by artist tag

This commit is contained in:
2026-08-22 01:58:13 +02:00
parent bae8adea05
commit e739ed3294
7 changed files with 35 additions and 6 deletions
Regular → Executable
+35 -6
View File
@@ -1,20 +1,26 @@
use clap::Parser;
use lofty::file::TaggedFileExt;
use lofty::probe::Probe;
use std::fs::rename;
use lofty::tag::Accessor;
use lofty::tag::Tag;
use std::fs::{create_dir_all, rename};
use std::path::PathBuf;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
file: PathBuf,
#[arg(short)]
import: bool,
#[arg(short)]
show: bool,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let file_path = args.file;
let target_dir = PathBuf::from("./test");
let target = target_dir.join(&file_path);
let tagged_file = Probe::open(&file_path)
.expect("ERROR: Bad path provided!")
@@ -22,6 +28,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.expect("ERROR: Failed to read file!");
let id3v2 = tagged_file.primary_tag();
let tag = match tagged_file.primary_tag() {
Some(primary_tag) => primary_tag,
None => tagged_file.first_tag().expect("ERROR: No tags found!"),
};
println!("{}", tag.title().as_deref().unwrap_or("None"));
let album_artist = tag.get_string(lofty::tag::ItemKey::AlbumArtist);
println!("AlbumArtist: {:?}", album_artist);
if let Some(tag) = id3v2 {
for item in tag.items() {
@@ -29,12 +44,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
let _ = move_files(target, file_path);
if args.import {
let _ = move_files(file_path, tag.clone());
}
Ok(())
}
fn move_files(target: PathBuf, file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
rename(file_path, target)?;
fn move_files(file_path: PathBuf, tag: Tag) -> Result<(), Box<dyn std::error::Error>> {
let target_dir = PathBuf::from("./test");
let artist = tag.artist();
let mut final_target = PathBuf::new();
if let Some(artist_path) = artist {
final_target = target_dir.join(artist_path.as_ref()).join(&file_path);
let dir_to_create = target_dir.join(artist_path.as_ref());
let _ = create_dir_all(dir_to_create);
}
println!("{:?}", final_target);
rename(file_path, final_target)?;
Ok(())
}