43 lines
1.7 KiB
Markdown
43 lines
1.7 KiB
Markdown
|
# rename-files
|
||
|
|
||
|
A CLI application that takes a regex patten for search and replace and then recursively renames files according to the regex pattern.
|
||
|
|
||
|
Uses `clap` to define and parse command-line arguments:
|
||
|
|
||
|
- `-s` or `--search`: The regex pattern to search for
|
||
|
- `-r` or `--replace`: The replacement pattern
|
||
|
- `-d` or `--directory`: The starting directory (optional, defaults to current directory)
|
||
|
- `--dry-run`: Option to preview changes without actually renaming files
|
||
|
|
||
|
Recursively walks through the directory structure using `walkdir`.
|
||
|
|
||
|
Applies the regex pattern to each file name and performs the replacement if there's a match.
|
||
|
|
||
|
Provides a dry-run option to preview changes before making them.
|
||
|
|
||
|
```bash
|
||
|
# Replace "old" with "new" in all file names
|
||
|
cargo run -- --search "old" --replace "new"
|
||
|
|
||
|
# Replace numbers with "X" in all file names, starting from a specific directory
|
||
|
cargo run -- --search "[0-9]" --replace "X" --directory "/path/to/dir"
|
||
|
|
||
|
# Preview changes without actually renaming (dry run)
|
||
|
cargo run -- --search "test_" --replace "" --dry-run
|
||
|
```
|
||
|
|
||
|
The program will:
|
||
|
|
||
|
- Only rename files (not directories)
|
||
|
- Show you what changes it's making
|
||
|
- Handle errors gracefully
|
||
|
- Allow you to preview changes with --dry-run before making actual changes
|
||
|
|
||
|
Safety features:
|
||
|
|
||
|
- Checks if the new filename would be different before attempting to rename
|
||
|
- Uses proper error handling throughout
|
||
|
- Provides feedback about what's being renamed
|
||
|
- Allows preview of changes before making them
|
||
|
|
||
|
> Remember that regex patterns are powerful and should be used carefully, especially when batch renaming files. It's recommended to always use the --dry-run option first to preview the changes before actually renaming files.
|