
Thousands of files in one Downloads or Scans folder? This script files them into 2019/06-Jun style folders automatically.
from pathlib import Path
from datetime import datetime
import shutil
SOURCE = Path(r"C:\Users\me\Downloads")
DRY_RUN = True # set to False to actually move files
for f in SOURCE.iterdir():
if not f.is_file():
continue
when = datetime.fromtimestamp(f.stat().st_mtime)
target = SOURCE / str(when.year) / when.strftime("%m-%b")
target.mkdir(parents=True, exist_ok=True) if not DRY_RUN else None
dest = target / f.name
n = 1
while dest.exists(): # never overwrite
dest = target / f"{f.stem} ({n}){f.suffix}"
n += 1
print(("WOULD MOVE " if DRY_RUN else "MOVED ") + f.name, "->", dest.relative_to(SOURCE))
if not DRY_RUN:
shutil.move(str(f), str(dest))
How it works
st_mtimeis the last-modified time — for downloads that is usually when the file arrived.- The
while dest.exists()loop adds (1), (2)… instead of overwriting duplicates. - Dry run first: read the printed plan, then set
DRY_RUN = False.
💡 Sort photos by the date they were taken instead: read the EXIF “DateTimeOriginal” tag with the Pillow library and fall back to st_mtime when it is missing.
Schedule it weekly with Windows Task Scheduler and the folder stays tidy on its own.
✨ Ask AI about this article
Stuck on a step? Ask a question and the AI answers using this article.
Free · AI can be wrong