Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Last active January 4, 2018 19:53
Show Gist options
  • Save santiago-salas-v/7f001a8d80534ebf9ec5104d409420bf to your computer and use it in GitHub Desktop.
Save santiago-salas-v/7f001a8d80534ebf9ec5104d409420bf to your computer and use it in GitHub Desktop.
Script to generate copies of files named 'fb_YYYY-MM-DD-hh-mm-ss_fbidxxxxxxxxx.jpg' . Copy them all to ./output and change modified date to the date referred to in the name. 2nd script with format "Whatsapp Image YYYY-MM-DD at hh-mm-ss"
import re
import os
import datetime
import time
from shutil import copyfile
regex_filename = \
'fb_([0-9]{4})-([0-9]{2})-([0-9]{2})' + \
'T([0-9]{2})-([0-9]{2})-([0-9]{2})_([0-9]+).jpg'
prog = re.compile(regex_filename)
files_list = os.listdir(os.curdir)
output_dir_name = os.path.join(os.curdir, 'output')
if not os.path.exists(output_dir_name):
os.mkdir(output_dir_name)
k = 0
for file_name in files_list:
match = prog.match(file_name)
if match:
k += 1
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
hour24 = int(match.group(4))
minute60 = int(match.group(5))
second60 = int(match.group(6))
fbid = match.group(7)
mtime = time.mktime(datetime.datetime(
year, month, day,
hour24, minute60, second60
).timetuple())
atime = time.mktime(datetime.datetime(
year, month, day,
hour24, minute60, second60
).timetuple())
new_file = os.path.join(
output_dir_name,
file_name
)
current_file = os.path.abspath(file_name)
copyfile(current_file, new_file)
os.utime(new_file, (atime, mtime))
print 'creating file ' + str(k) + ': ' + \
os.path.abspath(new_file) + '; ' + str(atime)
import re
import os
import datetime
import time
from shutil import copyfile
regex_filename = \
'([0-9]{4})[\.\-\,]([0-9]{2})[\.\-\,]([0-9]{2}).*' + \
'([0-9]{2})[\.\-\,]([0-9]{2})[\.\-\,]([0-9]{2}).*' + \
'jpe?g'
prog = re.compile(regex_filename)
files_list = os.listdir(os.curdir)
print(files_list)
output_dir_name = os.path.join(os.curdir, 'output')
if not os.path.exists(output_dir_name):
os.mkdir(output_dir_name)
k = 0
for file_name in files_list:
match = prog.search(file_name)
if match:
k += 1
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
hour24 = int(match.group(4))
minute60 = int(match.group(5))
second60 = int(match.group(6))
mtime = time.mktime(datetime.datetime(
year, month, day,
hour24, minute60, second60
).timetuple())
atime = time.mktime(datetime.datetime(
year, month, day,
hour24, minute60, second60
).timetuple())
new_file = os.path.join(
output_dir_name,
file_name
)
current_file = os.path.abspath(file_name)
copyfile(current_file, new_file)
os.utime(new_file, (atime, mtime))
print('creating file ' + str(k) + ': ' + \
os.path.abspath(new_file) + '; ' + str(atime))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment