batch_rename/
batch-rename.rs
1use std::{collections::HashSet, env, fs};
10use devela::{ExtPath, Path};
11
12fn main() {
13 let args: Vec<String> = env::args().collect();
14 let mut path = String::new();
15 let mut prefix = "";
16 let mut postfix = "";
17 let mut replace = "";
18 let mut replace_to = "";
19 let mut new_name = "";
20 let mut use_new_name = false;
21 let mut show_help = false;
22 let mut need_replace = false;
23
24 let mut options_exist = HashSet::new();
25
26 for i in 1..args.len() {
27 if args[i] == "--path" {
28 check_option_exist(&mut options_exist, &args[i]);
29 path = args[i + 1].clone();
30 if !path.ends_with("/") {
31 path.push(Path::SEPARATOR_CHAR);
32 }
33 } else if args[i] == "--prefix" {
34 check_option_exist(&mut options_exist, &args[i]);
35 prefix = &args[i + 1];
36 } else if args[i] == "--postfix" {
37 check_option_exist(&mut options_exist, &args[i]);
38 postfix = &args[i + 1];
39 } else if args[i] == "--replace" {
40 check_option_exist(&mut options_exist, &args[i]);
41 replace = &args[i + 1];
42 replace_to = &args[i + 2];
43 need_replace = true;
44 } else if args[i] == "--new" {
45 check_option_exist(&mut options_exist, &args[i]);
46 new_name = &args[i + 1];
47 use_new_name = true;
48 } else if args[i] == "--help" {
49 show_help = true;
50 }
51 }
52
53 if show_help {
54 println!("Usage: batch_rename [OPTIONS]\n");
55 println!("Options:");
56 println!(
57 " --path <path> The path to the directory containing the files to rename."
58 );
59 println!(" --prefix <prefix> The prefix to add to the file names.");
60 println!(" --postfix <postfix> The postfix to add to the file names.");
61 println!(
62 " --replace <old> <new> Replace the old string in the file names with the new string."
63 );
64 println!(" --new <name> Use a new name for the files, with a number appended to each file name.");
65 println!(" --help Print this help message.");
66 return;
67 } else if path.is_empty() {
68 println!("The --path parameter is required.");
69 return;
70 }
71
72 let files = fs::read_dir(&path).unwrap();
73 let mut count = 1;
74 for file in files {
75 let file_name = file.unwrap().file_name().into_string().unwrap();
76 let new_file_name = if use_new_name {
77 format!(
78 "{}{}.{}",
79 new_name,
80 count,
81 file_name.split('.').next_back().unwrap()
82 )
83 } else if need_replace {
84 let file_name = file_name.replace(replace, replace_to);
85 let x: Vec<&str> = file_name.split(".").collect();
86 format!("{}{}{}.{}", prefix, x[0], postfix, x[1])
87 } else {
88 let x: Vec<&str> = file_name.split(".").collect();
89 format!("{}{}{}.{}", prefix, x[0], postfix, x[1])
90 };
91 fs::rename(
92 format!("{}/{}", path, file_name),
93 format!("{}/{}", path, new_file_name),
94 )
95 .unwrap();
96 count += 1;
97 }
98}
99
100fn check_option_exist<'a>(options_exist: &mut HashSet<&'a String>, option: &'a String) {
101 if !options_exist.insert(option) {
102 panic!(
103 "Option {} has been set more than ones, please check the options",
104 option
105 );
106 }
107}