Skip to content

Instantly share code, notes, and snippets.

@rene-d
Last active December 30, 2018 05:58
Show Gist options
  • Save rene-d/a67d7e7e850a7b29a478a106b12adefa to your computer and use it in GitHub Desktop.
Save rene-d/a67d7e7e850a7b29a478a106b12adefa to your computer and use it in GitHub Desktop.
set file date based on the pattern in the filename
#! /usr/bin/env python3
"""
set file atime and ctime based on the date pattern YYYY[_-]MM[_-]DD in filename
"""
import argparse
import re
import os
import datetime
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs='+')
args = parser.parse_args()
for file in args.files:
m = re.search(r'(20[012]\d)[_\-]?(\d\d)[_\-]?(\d\d)', file)
if m:
y, m, d = map(int, m.groups())
t = datetime.datetime(y, m, d, 12)
os.utime(file, (t.timestamp(), t.timestamp()))
print("{:80} {}".format(file, t.strftime("%Y-%m-%d")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment