delete empty dirs

This commit is contained in:
Max Bradbury 2023-05-04 18:54:26 +01:00
parent 825482ba07
commit 7c26c38ccb
2 changed files with 30 additions and 26 deletions

2
Cargo.lock generated
View File

@ -45,7 +45,7 @@ dependencies = [
[[package]] [[package]]
name = "download-organiser" name = "download-organiser"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"dirs", "dirs",
"fs_extra", "fs_extra",

View File

@ -1,5 +1,5 @@
use std::fs::DirEntry; use std::fs::DirEntry;
use std::io; use std::{fs, io};
use std::path::PathBuf; use std::path::PathBuf;
use lazy_static::lazy_static; use lazy_static::lazy_static;
@ -83,6 +83,7 @@ fn handle_mp3(file: DirEntry) {
if let Some(tag) = meta.unwrap().tag { if let Some(tag) = meta.unwrap().tag {
println!("----------------------"); println!("----------------------");
println!("file: {:?}", file.path());
println!("artist: {}", tag.artist.trim()); println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim()); println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim()); println!("title: {}", tag.title.trim());
@ -152,10 +153,12 @@ fn handle_video(file: DirEntry) {
} }
fn handle_dir(path: PathBuf) { fn handle_dir(path: PathBuf) {
let dir = std::fs::read_dir(path).expect("Couldn't read dir"); let mut dir = std::fs::read_dir(&path).expect("Couldn't read dir");
// todo if dir is empty, delete
if dir.next().is_none() {
println!("Deleting empty dir: {:?}", path);
fs::remove_dir(path).expect("Couldn't delete dir")
} else {
for inode in dir { for inode in dir {
let inode = inode.expect("Couldn't read inode"); let inode = inode.expect("Couldn't read inode");
@ -182,6 +185,7 @@ fn handle_dir(path: PathBuf) {
} }
} }
} }
}
} }
fn main() { fn main() {