diff --git a/Automations/RenPy/README.md b/Automations/RenPy/README.md new file mode 100644 index 0000000..12fadd8 --- /dev/null +++ b/Automations/RenPy/README.md @@ -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 ` { `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 [-r ] [-s] + ``` +- **Example**: + ```bash + python ren.py C:\Users\YourName\Documents\Folder "File" + ``` + +#### Optional Arguments + +- `-r `: 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 -r alphabet + ``` + +- Rename files from newest to oldest: + + ```bash + python ren.py -r new + ``` + +- Rename files from oldest to newest (default): + ```bash + python ren.py -r old + ``` + +--- + +## ✨ Created by + +[MFM-347](https://github.com/MFM-347) diff --git a/Automations/RenPy/ren.py b/Automations/RenPy/ren.py new file mode 100644 index 0000000..13ac15f --- /dev/null +++ b/Automations/RenPy/ren.py @@ -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 -r " + ) + 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}-' in {args.order} order. No changes will be made.") + else: + rename_files(args.base_name, args.directory, args.order) + +if __name__ == "__main__": + main() diff --git a/README.md b/README.md index 8d62ac7..08c3004 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

- +

👩‍💻Awesome Automations👨‍💻