Normalize timestamp group folder names

This commit is contained in:
ajp_anton
2026-06-04 06:09:49 +00:00
parent 739b443bd2
commit 7ce356d299
2 changed files with 43 additions and 7 deletions
+14 -2
View File
@@ -12,11 +12,23 @@ Files are sorted by their current path and moved into one folder named
from __future__ import annotations
import os
import re
import shutil
import sys
from pathlib import Path
ORDERED_TIMESTAMP_STEM_RE = re.compile(r"^(?P<timestamp>\d{8}_\d{6})-[1-9]\d*$")
def group_stem(path: Path) -> str:
stem = path.stem
match = ORDERED_TIMESTAMP_STEM_RE.match(stem)
if match:
return match.group("timestamp")
return stem
def unique_path(path: Path) -> Path:
if not path.exists():
return path
@@ -87,8 +99,8 @@ def group_items(items: list[Path]) -> Path | None:
return None
sorted_items = sorted(items, key=lambda item: item.name.lower())
first = sorted_items[0].stem
last = sorted_items[-1].stem
first = group_stem(sorted_items[0])
last = group_stem(sorted_items[-1])
target_dir = unique_path(working_dir_for_items(sorted_items) / f"{first}-{last}")
target_dir.mkdir(parents=True)