Categories
Code

Renaming Exported Mac Photos directories to be in date order

Recently I had to work out a script that would rename the directory names that Photos exported to something that was sortable by date.

Something like

2013 Photo Export/Westborough, MA - W Park Dr, January 30, 2013

would go to

2013 Photo Export/20130130, Westborough, MA - W Park Dr

That way the directory could sort on 20130130!

Just update the WORKING_DIR to the directory that contains the exported photos and use this.


from dateutil.parser import parse
import datetime, os, shutil
WORKING_DIR = ''

def rename_dir_to_name(direc):
tokens = direc.split(‘ ‘)

if len(tokens) == 3:
#name should be it
dirtokens = [ ]
datetokens = [x.replace(“,”, “”) for x in tokens]
else:
dirtokens = tokens[:-3]
dirtokens[-1] = dirtokens[-1].replace(“,”, “”)
datetokens = [x.replace(“,”, “”) for x in tokens[-3:]]

if len(dirtokens) > 0:
dir_str = ” “.join(dirtokens)
else:
dir_str = “”

date_str = ” “.join(datetokens)

d = parse(date_str)

dir_name = datetime.datetime.strftime(d, “%Y%m%d”)

if len(tokens) > 3:
dir_name = dir_name + “, ” + dir_str

return dir_name

for file_name in os.listdir(WORKING_DIR):
if os.path.isdir(WORKING_DIR + ‘/’ + file_name):
print “Moving ” + WORKING_DIR + ‘/’ + file_name + ” to ” + WORKING_DIR + ‘/’ + rename_dir_to_name(file_name)
shutil.move(WORKING_DIR + ‘/’ + file_name, WORKING_DIR + ‘/’ + rename_dir_to_name(file_name))

Leave a Reply