Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import shutil
- import calendar
- from datetime import datetime
- # Base path
- base_path = r"D:\OneDrive\Photos Collection Since 2007\2017\Honor Israel 6x 2017"
- timestamp = datetime.now().strftime("%Y_%m_%d")
- log_file = os.path.join(base_path, f"organizer_log_{timestamp}.txt")
- # List of regex patterns for jpg filenames
- patterns = [
- re.compile(r"^IMG_(\d{8})_\d{6}\.jpg$", re.IGNORECASE),
- re.compile(r"^IMG_(\d{8})_\d{9}\.jpg$", re.IGNORECASE),
- re.compile(r"^IMG_(\d{8})_\d{9}\_HDR.jpg$", re.IGNORECASE),
- re.compile(r"^IMG_(\d{8})_\d{6}_\d+\.jpg$", re.IGNORECASE),
- # NEW PATTERNS ADDED HERE FOR AGC FILES
- # Matches AGC_YYYYMMDD_SEQUENCE.jpg (e.g., AGC_20240422_185307583.jpg)
- re.compile(r"^AGC_(\d{8})_\d{9}\.jpg$", re.IGNORECASE),
- # Matches AGC_YYYYMMDD_SEQUENCE.NIGHT.jpg (e.g., AGC_20241017_184801783.NIGHT.jpg)
- re.compile(r"^AGC_(\d{8})_\d{9}\.NIGHT\.jpg$", re.IGNORECASE),
- re.compile(r"^PXL_(\d{8})_.*\.jpg$", re.IGNORECASE),
- re.compile(r"^PXL_(\d{8})_\d{9}_\d+\.jpg$", re.IGNORECASE),
- re.compile(r"^PHOTO_(\d{8})_\d{6}\.jpg$", re.IGNORECASE),
- re.compile(r"^PIC(\d{8})_\d{6}\.jpg$", re.IGNORECASE)
- ]
- # Function to check against multiple patterns
- def match_patterns(filename):
- for p in patterns:
- m = p.match(filename)
- if m:
- return m
- return None
- # Function to write logs
- def write_log(message):
- timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
- with open(log_file, "a", encoding="utf-8") as f:
- f.write(f"{timestamp} {message}\n")
- try:
- # Walk recursively through the directory
- for root, dirs, files in os.walk(base_path):
- for file in files:
- try:
- file_path = os.path.join(root, file)
- # Skip if file is already in the target folders (avoid infinite loops)
- if any(folder in file_path.lower() for folder in ["\\others\\", "\\temp\\"]):
- continue
- # Check if file matches any of the jpg patterns
- match = match_patterns(file)
- if match:
- date_str = match.group(1) # YYYYMMDD
- year = date_str[:4]
- month_num = date_str[4:6]
- month_name = calendar.month_name[int(month_num)]
- month_folder = f"{month_num} - {month_name}"
- # Destination: d:\photos\year\MM - Month
- dest_dir = os.path.join(base_path, year, month_folder)
- os.makedirs(dest_dir, exist_ok=True)
- dest_path = os.path.join(dest_dir, file)
- shutil.move(file_path, dest_path)
- write_log(f"MOVED JPG (pattern) → {dest_path}")
- elif file.lower().endswith(".jpg"):
- # If JPG but not matching pattern → move to temp
- temp_dir = os.path.join(base_path, "temp")
- os.makedirs(temp_dir, exist_ok=True)
- dest_path = os.path.join(temp_dir, file)
- shutil.move(file_path, dest_path)
- write_log(f"MOVED JPG (no pattern) → {dest_path}")
- else:
- # Other file types → move to d:\photos\others\ext
- ext = os.path.splitext(file)[1].lower().lstrip(".")
- if not ext: # No extension
- ext = "noext"
- dest_dir = os.path.join(base_path, "others", ext)
- os.makedirs(dest_dir, exist_ok=True)
- dest_path = os.path.join(dest_dir, file)
- shutil.move(file_path, dest_path)
- write_log(f"MOVED OTHER ({ext}) → {dest_path}")
- except Exception as e:
- write_log(f"ERROR processing file {file}: {e}")
- except Exception as e:
- write_log(f"FATAL ERROR: {e}")
Advertisement
Add Comment
Please, Sign In to add comment