Wednesday, June 10, 2026

How to Bulk Rename Hundreds of Files Instantly Using Python

If you deal with folders full of messy files like IMG_0023.jpg, document_v1_final.docx, or generic scanned invoices, renaming them one by one is a massive waste of time.

You can automate this entire process instantly by writing a simple Python script using the built-in os library. Whether you want to add today's date to every file, add a specific prefix, or sequentially number them, this automation engine handles it in milliseconds.

Step 1: The Bulk Rename Code Structure

You do not need to install any external third-party libraries. Open your favorite code editor (like VS Code or PyCharm), create a file named renamer.py, and paste the following code framework:

import os
from datetime import datetime

# Define the directory containing the files you want to rename
target_dir = os.path.expanduser("~/Downloads/Invoices")

def bulk_rename_files(prefix="Document", add_date=True):
    # Safety check: exit if the folder doesn't exist
    if not os.path.exists(target_dir):
        print(f"Error: The directory '{target_dir}' was not found.")
        return

    # Get the current date in YYYY-MM-DD format
    current_date = datetime.now().strftime("%Y-%m-%d")
    
    # Counter for sequential numbering
    counter = 1
    
    print("Starting bulk rename audit...")
    
    for filename in sorted(os.listdir(target_dir)):
        old_file_path = os.path.join(target_dir, filename)
        
        # Skip directories to avoid breaking folders
        if os.path.isdir(old_file_path):
            continue
            
        # Extract the file extension (e.g., .pdf, .jpg)
        _, ext = os.path.splitext(filename)
        
        # Construct the clean, new filename
        if add_date:
            new_filename = f"{prefix}_{current_date}_{counter}{ext}"
        else:
            new_filename = f"{prefix}_{counter}{ext}"
            
        new_file_path = os.path.join(target_dir, new_filename)
        
        # Execute the rename safely
        os.rename(old_file_path, new_file_path)
        print(f"Renamed: {filename} -> {new_filename}")
        
        counter += 1
        
    print(f"Success! Safely renamed {counter - 1} files.")

if __name__ == "__main__":
    # Customize your prefix here
    bulk_rename_files(prefix="Invoice", add_date=True)

Step 2: How to Deploy and Run the Script

  • Save the file on your computer as renamer.py.
  • Change the target_dir path in the code to point to your messy folder.
  • Open your terminal (macOS/Linux) or Command Prompt (Windows).
  • Navigate to the directory where your script lives using cd.
  • Execute the automation engine by typing:
python renamer.py

Step 3: What Happens Under the Hood?

  • datetime.now(): Automatically grabs the exact date of execution so your files are perfectly time-stamped.
  • sorted(os.listdir()): Sorts your files alphabetically before renaming them, ensuring your sequential numbering stays in perfect order.
  • os.rename(): Modifies the file metadata directly on your storage drive instantly without consuming heavy RAM or CPU cycles.

No comments:

Post a Comment