Skip to content

Instantly share code, notes, and snippets.

@jpz
Created December 15, 2016 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpz/9a672f1e13ae6f919bb024ce87a49773 to your computer and use it in GitHub Desktop.
Save jpz/9a672f1e13ae6f919bb024ce87a49773 to your computer and use it in GitHub Desktop.
Touches the creation times (st_birthtime) back to the modified time for files on MacOS, where the creation time is greater than the modified datetime
#!/usr/bin/env python3
# this small script touches the creation times (st_birthtime) back to the modified time
# for files on MacOS, where the creation time is greater than the modified datetime.
#
# fixes a small problem I had with some very old photo files...
from subprocess import check_output, PIPE
import sys, dateparser
def get_creation_date(filename):
date = check_output(['GetFileInfo', '-d', filename], stdin=PIPE).decode("utf-8")
return dateparser.parse(date), date
def get_modification_date(filename):
date = check_output(['GetFileInfo', '-m', filename], stdin=PIPE).decode("utf-8")
return dateparser.parse(date), date
def set_creation_date(filename, date):
return dateparser.parse(check_output(['SetFile', '-d', date, filename], stdin=PIPE).decode("utf-8"))
for file in sys.argv[1:]:
crdate, crdatestr = get_creation_date(file)
moddate, moddatestr = get_modification_date(file)
print("checking " + file + ": ", end="")
if crdate > moddate:
print(crdate, moddate)
set_creation_date(file, moddatestr)
else:
print("OK")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment