Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RenPy for File Renaming Automation #97

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Automations/RenPy/README.md
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)
72 changes: 72 additions & 0 deletions Automations/RenPy/ren.py
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()
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_RATvLqD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1600/1%2A1hT23VteSYhRbOaUtCcuEg.gif" width="700" height="300" />
<img src="https://cdn-images-1.medium.com/v2/resize:fit:1600/1*1hT23VteSYhRbOaUtCcuEg.gif" width="700" height="300" />
</p>

<h1 align="center">👩‍💻Awesome Automations👨‍💻</h1>
Expand Down
Loading