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]]
name = "download-organiser"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"dirs",
"fs_extra",

View File

@ -1,5 +1,5 @@
use std::fs::DirEntry;
use std::io;
use std::{fs, io};
use std::path::PathBuf;
use lazy_static::lazy_static;
@ -83,6 +83,7 @@ fn handle_mp3(file: DirEntry) {
if let Some(tag) = meta.unwrap().tag {
println!("----------------------");
println!("file: {:?}", file.path());
println!("artist: {}", tag.artist.trim());
println!("album: {}", tag.album.trim());
println!("title: {}", tag.title.trim());
@ -152,33 +153,36 @@ fn handle_video(file: DirEntry) {
}
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 {
let inode = inode.expect("Couldn't read inode");
// recursively handle directories
if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
// recursively handle directories
if inode.metadata().unwrap().is_dir() {
handle_dir(inode.path());
}
if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
match extension.to_string().to_lowercase().as_ref() {
"gif" => { handle_gif( inode) }
"jpg" => { handle_image(inode) }
"jpeg" => { handle_image(inode) }
"png" => { handle_image(inode) }
"mp3" => { handle_mp3( inode) }
"pdf" => { handle_pdf( inode) }
"avi" => { handle_video(inode) }
"m4v" => { handle_video(inode) }
"mkv" => { handle_video(inode) }
"mp4" => { handle_video(inode) }
// todo m4a, flac, etc?
// todo pdf, other documents
_ => { /*println!("Here's where we would do nothing.");*/ }
if let Some(extension) = inode.path().extension().unwrap_or("none".as_ref()).to_str() {
match extension.to_string().to_lowercase().as_ref() {
"gif" => { handle_gif( inode) }
"jpg" => { handle_image(inode) }
"jpeg" => { handle_image(inode) }
"png" => { handle_image(inode) }
"mp3" => { handle_mp3( inode) }
"pdf" => { handle_pdf( inode) }
"avi" => { handle_video(inode) }
"m4v" => { handle_video(inode) }
"mkv" => { handle_video(inode) }
"mp4" => { handle_video(inode) }
// todo m4a, flac, etc?
// todo pdf, other documents
_ => { /*println!("Here's where we would do nothing.");*/ }
}
}
}
}