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,33 +153,36 @@ 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 {
let inode = inode.expect("Couldn't read inode");
for inode in dir { // recursively handle directories
let inode = inode.expect("Couldn't read inode"); if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
// recursively handle directories if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
if inode.metadata().unwrap().is_dir() { match extension.to_string().to_lowercase().as_ref() {
handle_dir(inode.path()); "gif" => { handle_gif( inode) }
} "jpg" => { handle_image(inode) }
"jpeg" => { handle_image(inode) }
if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() { "png" => { handle_image(inode) }
match extension.to_string().to_lowercase().as_ref() { "mp3" => { handle_mp3( inode) }
"gif" => { handle_gif( inode) } "pdf" => { handle_pdf( inode) }
"jpg" => { handle_image(inode) } "avi" => { handle_video(inode) }
"jpeg" => { handle_image(inode) } "m4v" => { handle_video(inode) }
"png" => { handle_image(inode) } "mkv" => { handle_video(inode) }
"mp3" => { handle_mp3( inode) } "mp4" => { handle_video(inode) }
"pdf" => { handle_pdf( inode) } // todo m4a, flac, etc?
"avi" => { handle_video(inode) } // todo pdf, other documents
"m4v" => { handle_video(inode) } _ => { /*println!("Here's where we would do nothing.");*/ }
"mkv" => { handle_video(inode) } }
"mp4" => { handle_video(inode) }
// todo m4a, flac, etc?
// todo pdf, other documents
_ => { /*println!("Here's where we would do nothing.");*/ }
} }
} }
} }