
📎 This article includes 2 downloadable practice files ↓
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.
Schedule it weekly with Windows Task Scheduler and the folder stays tidy on its own.
📎 Practice files for this article
⬇ Download all 2 files (ZIP · 1 KB)
- 🐍organise_by_date.pyThe finished script with a safe preview mode and no-overwrite renaming.⬇ PY · 1 KB
- 🗂️Sample folder (12 files, different dates)Unzip, run the script in preview mode, then with --move. Dates are preserved in the zip.⬇ ZIP · 2 KB
Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.
Atul (AJ) builds office-automation and local-first AI tools as Atulya AI, and works with Excel, SAP and Oracle EPM every day. He writes about the shortcuts, macros and automations that actually save time — plus the occasional wacky fact. Videos on YouTube: Lazy Automator AI (@LazyAutomatorAI).
More posts by Atul →Stuck on a step? Ask a question and the AI answers using this article.