#!python3 # https://github.com/hMatoba/Piexif # http://ce3wiki.theturninggate.net/doku.php?id=file_name_convention # destination: // /ab---.jpg from pathlib import Path import datetime as dt import piexif from collections import defaultdict SRC = r'E:\MediaByTarget\Plex\Home Videos' DEST = r'E:\Users\Andreas\RawByDate' author = 'ab' dry_run = False def constant_factory(value): return lambda: value dateidx = defaultdict(constant_factory(0)) for suffix in ("jpg", "gif", "png", "mp4", "avi", "mov"): for raw in Path(SRC).rglob(f"*.{suffix}"): fn = raw.name if not raw.is_file(): print(f"skipped {fn}") continue try: exif_dict = piexif.load(str(raw)) idto = exif_dict["Exif"][piexif.ExifIFD.DateTimeOriginal].decode("ascii") rawdt = dt.datetime.strptime(idto,"%Y:%m:%d %H:%M:%S") except: print(f'exif failed {fn}, using file date') mtime = raw.stat().st_mtime rawdt = dt.datetime.fromtimestamp(mtime) year = rawdt.year day = f"{rawdt:%Y%m%d}" ts = f"{rawdt:%H%M%S}" destdir = Path(DEST, f"{year}",f"{day}") if not destdir.exists() and not dry_run: print(f'creating {destdir}') destdir.mkdir(parents=True) destfn = destdir / f"{author}-{day}-{ts}{raw.suffix}" if destfn.exists(): index = dateidx[day] + 1 dateidx[day] = index destfn = destdir / f"{author}-{day}-{index:04}{raw.suffix}" print(fn, "->", destfn) if not dry_run: raw.rename(destfn)