Last active
December 30, 2018 05:58
-
-
Save rene-d/a67d7e7e850a7b29a478a106b12adefa to your computer and use it in GitHub Desktop.
set file date based on the pattern in the filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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