Skip to content

Instantly share code, notes, and snippets.

@0x9900
Last active June 28, 2024 22:34
Show Gist options
  • Save 0x9900/b08005324ae54c2c18ad6906cbb7c7cf to your computer and use it in GitHub Desktop.
Save 0x9900/b08005324ae54c2c18ad6906cbb7c7cf to your computer and use it in GitHub Desktop.
Rename images
#! /usr/bin/env python
# vim:fenc=utf-8
#
# Copyright © 2024 fred <github-fred@hidzz.com>
#
# Distributed under terms of the BSD 3-Clause license.
"""
Rename all the DXCC image with the new date format
"""
import pathlib
import re
import sys
from datetime import datetime, timezone
from typing import Any
R_DATE = re.compile(r'(?P<name>.*)-(?P<dte>\d+)\.(png|svgz)').match
def parse_file(name: str) -> tuple[str, str] | None:
if match := R_DATE(name):
date = datetime.strptime(match.group('dte'), '%Y%m%d%H%M')
date = date.replace(tzinfo=timezone.utc)
return date.strftime('%Y%m%dT%H%M%S'), match.group('name')
return None
def rename_files(path: pathlib.Path) -> None:
for filename in path.iterdir():
match = parse_file(filename.name)
if match is None:
continue
date, name = match[0], match[1]
ext = filename.suffix
target = filename.parent.joinpath(f"{name}-{date}{ext}")
if not target.exists():
filename.rename(target)
print(f'{filename} -> {target} ')
def main() -> None:
if len(sys.argv) < 2:
print(f"{__file__} path ...")
raise SystemExit('Missing path')
for _path in sys.argv[1:]:
path = pathlib.Path(_path)
rename_files(path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment