Rust 语言 编写批量重命名工具 支持正则 / 时间戳重命名

Rust阿木 发布于 1 天前 1 次阅读


Rust语言批量重命名工具:正则与时间戳的灵活应用

在处理文件时,批量重命名是一个常见的任务。无论是为了组织文件结构,还是为了满足特定的命名规范,批量重命名都能大大提高工作效率。Rust语言以其高性能和安全性而闻名,非常适合编写这类工具。本文将介绍如何使用Rust语言编写一个支持正则表达式和时间戳重命名的批量重命名工具。

Rust语言简介

Rust是一种系统编程语言,旨在提供内存安全、并发和性能。它具有以下特点:

- 内存安全:Rust通过所有权(ownership)、借用(borrowing)和生命周期(lifetimes)等机制确保内存安全。
- 并发:Rust提供了强大的并发编程工具,如通道(channels)和锁(locks)。
- 性能:Rust的性能接近C/C++,同时提供了更高的安全性和更简单的抽象。

工具设计

我们的批量重命名工具将支持以下功能:

- 支持正则表达式重命名
- 支持基于时间戳的重命名
- 支持递归重命名目录中的所有文件
- 提供用户友好的命令行界面

技术选型

为了实现上述功能,我们将使用以下Rust库:

- `regex`:用于正则表达式匹配和替换。
- `chrono`:用于处理时间戳。
- `clap`:用于构建命令行界面。

代码实现

以下是批量重命名工具的Rust代码实现:

rust
use std::env;
use std::fs;
use std::path::Path;
use regex::Regex;
use chrono::{DateTime, Utc, Local};
use clap::{App, Arg};

fn main() {
let matches = App::new("Batch Rename Tool")
.version("1.0")
.author("Your Name")
.about("A tool for batch renaming files using regular expressions and timestamps.")
.arg(Arg::with_name("path")
.short("p")
.long("path")
.value_name("PATH")
.help("The path to the directory containing the files to rename.")
.required(true))
.arg(Arg::with_name("pattern")
.short("r")
.long("regex")
.value_name("PATTERN")
.help("The regular expression pattern to match and replace in the file names.")
.required(false))
.arg(Arg::with_name("timestamp")
.short("t")
.long("timestamp")
.help("Use timestamp for renaming files.")
.required(false))
.get_matches();

let path = matches.value_of("path").unwrap();
let pattern = matches.value_of("pattern");
let use_timestamp = matches.is_present("timestamp");

if use_timestamp {
rename_files_with_timestamp(path);
} else if let Some(pattern) = pattern {
rename_files_with_regex(path, pattern);
} else {
println!("No renaming pattern provided.");
}
}

fn rename_files_with_regex(path: &str, pattern: &str) {
let regex = Regex::new(pattern).unwrap();
let entries = fs::read_dir(path).unwrap();

for entry in entries {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap().to_str().unwrap();
let new_name = regex.replace(file_name, "new_name");
let new_path = path.with_file_name(new_name);
fs::rename(&path, &new_path).unwrap();
println!("Renamed {} to {}", path.display(), new_path.display());
}
}
}

fn rename_files_with_timestamp(path: &str) {
let now: DateTime = Utc::now();
let timestamp = now.format("%Y%m%d%H%M%S").to_string();

let entries = fs::read_dir(path).unwrap();

for entry in entries {
let entry = entry.unwrap();
let path = entry.path();
if path.is_file() {
let file_name = path.file_name().unwrap().to_str().unwrap();
let new_name = format!("{}_{}", timestamp, file_name);
let new_path = path.with_file_name(new_name);
fs::rename(&path, &new_path).unwrap();
println!("Renamed {} to {}", path.display(), new_path.display());
}
}
}

使用说明

要使用这个工具,你需要将上述代码保存到一个`.rs`文件中,并使用`cargo`命令编译和运行它。以下是一些使用示例:

sh
cargo run -p /path/to/directory
cargo run -p /path/to/directory -r "s/old/new/g"
cargo run -p /path/to/directory -t

- `-p` 参数指定要重命名文件的目录路径。
- `-r` 参数指定正则表达式模式。
- `-t` 参数使用当前时间戳进行重命名。

总结

本文介绍了如何使用Rust语言编写一个支持正则表达式和时间戳重命名的批量重命名工具。通过使用Rust的内存安全特性和丰富的库支持,我们可以创建一个既安全又高效的工具。这个工具可以帮助用户轻松地重命名大量文件,提高文件管理的效率。