Categories We Write About

Automatically rename files based on metadata

Automatically renaming files based on metadata can be accomplished using scripts that extract specific metadata from each file (e.g., date created, author, file type, etc.) and rename the files accordingly. This is especially common for photo, video, and document files.

Here’s a general overview and example using Python:


Python Script to Rename Files Based on Metadata

For Images (Using EXIF Metadata)

python
import os from PIL import Image from PIL.ExifTags import TAGS from datetime import datetime def get_exif_data(image_path): image = Image.open(image_path) exif_data = {} info = image._getexif() if info: for tag, value in info.items(): decoded = TAGS.get(tag, tag) exif_data[decoded] = value return exif_data def rename_images_by_date(directory): for filename in os.listdir(directory): if filename.lower().endswith(('.jpg', '.jpeg', '.png')): full_path = os.path.join(directory, filename) exif = get_exif_data(full_path) if 'DateTimeOriginal' in exif: date_str = exif['DateTimeOriginal'] date_obj = datetime.strptime(date_str, '%Y:%m:%d %H:%M:%S') new_filename = date_obj.strftime('%Y-%m-%d_%H-%M-%S') + os.path.splitext(filename)[1] new_path = os.path.join(directory, new_filename) os.rename(full_path, new_path) print(f'Renamed to: {new_filename}') # Example usage rename_images_by_date('/path/to/your/images')

For Videos or Audio (Using Media Metadata with mutagen or hachoir)

python
from mutagen.easyid3 import EasyID3 import os def rename_audio_files(directory): for filename in os.listdir(directory): if filename.lower().endswith('.mp3'): try: path = os.path.join(directory, filename) audio = EasyID3(path) artist = audio.get('artist', ['Unknown'])[0] title = audio.get('title', ['Unknown'])[0] new_name = f"{artist} - {title}.mp3" os.rename(path, os.path.join(directory, new_name)) print(f"Renamed: {filename} -> {new_name}") except Exception as e: print(f"Error with file {filename}: {e}") # Example usage rename_audio_files('/path/to/your/audio')

For Documents (Using python-docx for Word files or PyPDF2 for PDFs)

python
import os from PyPDF2 import PdfReader def rename_pdfs_by_title(directory): for filename in os.listdir(directory): if filename.lower().endswith('.pdf'): path = os.path.join(directory, filename) try: reader = PdfReader(path) title = reader.metadata.title or 'Untitled' new_name = f"{title}.pdf".replace('/', '_') # Avoid illegal characters os.rename(path, os.path.join(directory, new_name)) print(f"Renamed to: {new_name}") except Exception as e: print(f"Could not process {filename}: {e}") # Example usage rename_pdfs_by_title('/path/to/your/pdfs')

Notes:

  • Always test scripts on copies of your data to avoid accidental data loss.

  • File naming collisions should be handled (e.g., appending a counter).

  • You may need additional libraries:

    bash
    pip install Pillow mutagen python-docx PyPDF2

If you need the script tailored for a specific file type or metadata field, I can provide that too.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About