【无用软件】使用随机字母+数字(共12位)来重命名你的图片

RT,这里是使用Rust写的一个小玩意,它唯一的作用就是重命名你拖到它上面的图片(或拖一个文件夹,重命名文件夹里面的图片)……

这玩意主要是与 @xiaokonglong 大佬汉化的 Dual Monitor Tools 一起使用,达到随机变换桌面壁纸的功效的(软件本身的随机不大行……)

仙人指路: 求一个本地壁纸加载软件 - #12,来自 xiaokonglong

没写错误处理(应该不会有人把system32文件夹的文件放到这里吧……),和避免重复的函数(应该不会有人的壁纸达到了 {26} ^ {12} 张吧)


大家自己编译吧,几秒钟的事,懒得发了……

开源如下(rust仍在入门中,部分代码写的很丑陋,不过没事,能跑就行):


二编:改了一点点rust代码

main.rs
use rand::distributions::Alphanumeric;
use rand::Rng;
use std::{env, fs, path::PathBuf};
fn main() {
    let mut args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        println!(
            "\nPic 12 Renamer    version 1.5\n\nUsage:\n{} <directory>\n{} <file(s)>\n",
            &args[0], &args[0]
        );
        return;
    }

    let input_path: PathBuf = PathBuf::from(&args[1]);
    let (input_is_dir, checked_path) = check_path_dir(input_path);
    if input_is_dir {
        println!("Enum {:?}", checked_path);
        for files in fs::read_dir(&checked_path).unwrap() {
            judge_rename(&files.unwrap().path());
        }
    } else {
        args.remove(0);
        for i in args {
            judge_rename(&PathBuf::from(&i));
        }
    }
}

fn check_path_dir(mut path_to_check: PathBuf) -> (bool, PathBuf) {
    path_to_check = if path_to_check.ends_with("\"") {
        PathBuf::from(path_to_check.to_string_lossy().strip_suffix("\"").unwrap())
    } else {
        path_to_check
    };

    if !path_to_check.exists() {
        panic!("{:?} does not exist.", path_to_check.to_str());
    }

    (
        path_to_check.is_dir(),
        path_to_check,
    )
}

fn generate_random_string() -> String {
    let mut rng = rand::thread_rng();
    let random_string: String = (&mut rng)
        .sample_iter(&Alphanumeric)
        .take(12)
        .map(char::from)
        .collect();
    random_string
}

fn judge_rename(each_path: &PathBuf) {
    if !each_path.is_dir() {
        let ext_list = ["png", "jpg", "jpeg"];
        let file_name = each_path.file_stem().unwrap().to_str().unwrap();
        let file_ext = each_path.extension().unwrap().to_str().unwrap();

        if ext_list.contains(&file_ext) {
            if file_name.len() != 12 {
                rename_files(each_path, file_ext);
            } else {
                if file_name.find(|c: char| c.is_ascii_punctuation()).is_some() {
                    rename_files(each_path, file_ext);
                }
            }
        }
    }
}

fn rename_files(before_path: &PathBuf, ext: &str) {
    let new_file_name = generate_random_string();
    let after_path = PathBuf::from(before_path.parent().unwrap().to_str().unwrap())
        .join(new_file_name + "." + ext);
    fs::rename(&before_path, &after_path).unwrap();
    println!("Renamed {:?} to {:?}", before_path, after_path);
}
cargo.toml
[package]
name = "_pic12_renamer"
version = "1.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.release]
lto = true
codegen-units = 1
opt-level = "z"
strip = true

[dependencies]
rand = "0.8.5"