-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8e59272
Showing
4 changed files
with
367 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "rmf" | ||
version = "1.0.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
clap = { version = "3.1.6", features = ["derive"] } | ||
chrono = "0.4.19" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::{path::Path, time::{self, UNIX_EPOCH}, fs}; | ||
|
||
use chrono::Local; | ||
use clap::Parser; | ||
|
||
/// 删除指定路径下面的所有文件 | ||
#[derive(Parser, Debug)] | ||
#[clap( | ||
name = "rmf", | ||
version = "1.0.0", | ||
author = "作者: 技安 <[email protected]>", | ||
about = "删除指定路径下面的所有文件, 帮助输入 `-h`", | ||
long_about = None, | ||
arg_required_else_help(true) | ||
)] | ||
struct Cli { | ||
/// 要删除的文件上级路径 | ||
#[clap(short, long)] | ||
path: String, | ||
|
||
/// 保留最近文件的天数 | ||
#[clap(short, long, default_value = "1")] | ||
days: u32, | ||
} | ||
|
||
fn main() { | ||
let cli = Cli::parse(); | ||
|
||
let path = Path::new(&cli.path); | ||
if !path.is_dir() { | ||
println!("{} is not a directory", cli.path); | ||
return; | ||
} | ||
|
||
// 开始时间 | ||
let start = time::Instant::now(); | ||
|
||
// 现在的时间 | ||
let timestamp = if cli.days > 0 { | ||
Local::today().and_hms(0, 0, 0).timestamp() - ((cli.days as i64 - 1) * 24 * 60 * 60) | ||
} else { | ||
Local::now().timestamp() + 100 | ||
}; | ||
|
||
for dir_entry in path.read_dir() | ||
.expect("Unable to read directory"). | ||
into_iter() { | ||
if let Ok(dir_entry) = dir_entry { | ||
if let Ok(metadata) = dir_entry.metadata() { | ||
if let Ok(time) = metadata.modified() { | ||
if let Ok(time) = time.duration_since(UNIX_EPOCH) { | ||
if (time.as_secs() as i64) < timestamp { | ||
if metadata.is_file() { | ||
fs::remove_file(dir_entry.path()).expect("Unable to remove file"); | ||
} else if metadata.is_dir() { | ||
fs::remove_dir_all(dir_entry.path()).expect("Unable to remove directory"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// 用时 | ||
let elapsed = start.elapsed(); | ||
println!("duration: {:?}", elapsed); | ||
} |