-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from MFM-347/dev
Add RenPy for File Renaming Automation
- Loading branch information
Showing
3 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# RenPy: File Renaming Automation | ||
|
||
RenPy is a Python CLI Program written to automate the renaming of files in a given directory. It sequentially renames files using a specified base name, appending a numeric index to each file while preserving its original file extension. | ||
|
||
--- | ||
|
||
## 🛠️ Pre-requisites | ||
|
||
- Python 3.x | ||
- Basic knowledge of terminal or command prompt usage. | ||
|
||
### 📝 Additional packages: | ||
|
||
- pyfiglet | ||
|
||
#### How do I install additional packages? 🤨 | ||
|
||
- Make sure you have python package manager (pip) installed on your system. | ||
- Go to command line and type `pip install <library name>` { `pip3` on mac }. | ||
- For detailed information see Python's official tutorial on [how to install packages](https://packaging.python.org/tutorials/installing-packages/). | ||
|
||
--- | ||
|
||
## ⚡ How It Works | ||
|
||
- The script takes two inputs: | ||
- **Directory**: The path to the directory containing files to be renamed. | ||
- **Base Name**: The prefix to use for the renamed files. | ||
- It sorts the files in the directory based on the specified order and renames them in sequence. | ||
|
||
For example: | ||
|
||
- Input directory contains files: `file1.txt`, `file2.txt`. | ||
- Base name is `Document`. | ||
- Output: `Document-1.txt`, `Document-2.txt`. | ||
|
||
--- | ||
|
||
### How do I use this program? 💻 | ||
|
||
- **Run the script**: | ||
```bash | ||
python ren.py <base_name> <directory> [-r <order>] [-s] | ||
``` | ||
- **Example**: | ||
```bash | ||
python ren.py C:\Users\YourName\Documents\Folder "File" | ||
``` | ||
|
||
#### Optional Arguments | ||
|
||
- `-r <order>`: Order of renaming. Options are `alphabet` for alphabetical order, `new` for newest-to-oldest order, and `old` for oldest-to-newest order (default). | ||
- `-s`: Simulate the renaming process without making changes. | ||
|
||
### Example Usage | ||
|
||
- Rename files in alphabetical order: | ||
|
||
```bash | ||
python ren.py <base_name> <directory> -r alphabet | ||
``` | ||
|
||
- Rename files from newest to oldest: | ||
|
||
```bash | ||
python ren.py <base_name> <directory> -r new | ||
``` | ||
|
||
- Rename files from oldest to newest (default): | ||
```bash | ||
python ren.py <base_name> <directory> -r old | ||
``` | ||
|
||
--- | ||
|
||
## ✨ Created by | ||
|
||
[MFM-347](https://github.com/MFM-347) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
import sys | ||
from pyfiglet import Figlet | ||
import argparse | ||
|
||
def banner(): | ||
f = Figlet(font="speed") | ||
print(f.renderText("RenPy")) | ||
|
||
def rename_files(base_name, directory, order): | ||
try: | ||
files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))] | ||
|
||
if order == "alphabet": | ||
files.sort() | ||
elif order == "new": | ||
files.sort(key=lambda f: os.path.getmtime(os.path.join(directory, f)), reverse=True) | ||
else: | ||
files.sort(key=lambda f: os.path.getmtime(os.path.join(directory, f))) | ||
|
||
for index, filename in enumerate(files, start=1): | ||
file_extension = os.path.splitext(filename)[1] | ||
new_name = f"{base_name}-{index}{file_extension}" | ||
old_file = os.path.join(directory, filename) | ||
new_file = os.path.join(directory, new_name) | ||
os.rename(old_file, new_file) | ||
print(f"Renamed: {filename} -> {new_name}") | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
|
||
def main(): | ||
banner() | ||
parser = argparse.ArgumentParser( | ||
description="RenPy is a Python CLI Program written to automate the renaming of files in a given directory.", | ||
epilog="Example usage:\n python ren.py <base_name> <directory> -r <order>" | ||
) | ||
parser.add_argument( | ||
"base_name", | ||
type=str, | ||
help="Base name for the renamed files", | ||
) | ||
parser.add_argument( | ||
"directory", | ||
type=str, | ||
help="Path to the directory containing files to be renamed", | ||
) | ||
parser.add_argument( | ||
"-r", "--order", | ||
type=str, | ||
choices=["alphabet", "new", "old"], | ||
default="old", | ||
help="Order of renaming: 'alphabet' for alphabetical, 'new' for newest-to-oldest, 'old' for oldest-to-newest (default)", | ||
) | ||
parser.add_argument( | ||
"-s", "--simulate", | ||
action="store_true", | ||
help="Simulate the renaming process without making changes", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
if not os.path.isdir(args.directory): | ||
print(f"Error: Directory '{args.directory}' does not exist.") | ||
sys.exit(1) | ||
|
||
if args.simulate: | ||
print(f"Simulation mode: Files in '{args.directory}' will be renamed to follow '{args.base_name}-<number>' in {args.order} order. No changes will be made.") | ||
else: | ||
rename_files(args.base_name, args.directory, args.order) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters