David Li
3 min readMay 13, 2023

--

title img

Introduction

In this article, we will explore how to create a command-line utility to batch rename files in a directory based on user-defined patterns or rules. We will use Rust’s standard library and the regex crate to handle complex renaming patterns.

Setting up the Project

To get started, we need to set up a new Rust project. To create a new Rust project, open a terminal window and run the following command:

$ cargo new file_renamer

This command will create a new Rust project named file_renamer. The project will contain a Cargo.toml file that describes the project's dependencies and a src directory that contains the project's source code.

The Cargo.toml file will look like this:

[package]
name = "file_renamer"
version = "0.1.0"
authors = ["Your Name <your.email@example.com>"]
edition = "2018"

[dependencies]

We will add dependencies to the Cargo.toml file as we need them.

Renaming Files

To rename files, we will use Rust’s standard library to handle basic file I/O and the regex crate to handle complex renaming patterns.

We will create a function named rename_files that will take a directory path and a pattern string as parameters. The function will read all files in the directory, apply the pattern to each file…

--

--