Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Last active September 16, 2018 21:23
Show Gist options
  • Save santiago-salas-v/cf402175aba3de16e99f69aaf2f209cd to your computer and use it in GitHub Desktop.
Save santiago-salas-v/cf402175aba3de16e99f69aaf2f209cd to your computer and use it in GitHub Desktop.
Dates on the name of Whatsapp pictures are applied to the file
import re
import os
import datetime
import time
from shutil import copyfile
regex_filename = \
r'([0-9]{4})([0-9]{2})([0-9]{2})' + \
'[\_|\-]([0-9]{2})([0-9]{2})([0-9]{2})' + \
'.*(\.jpe?g|\.png)' + \
'|' + \
'([0-9]{4})([0-9]{2})([0-9]{2})' + \
'\-WA([0-9]{4})' + \
'.*(\.jpe?g|\.png)'
prog = re.compile(regex_filename)
files_list = os.listdir(os.curdir)
print(files_list)
def timestruct_to_str(time_struct):
year = time_struct.tm_year
month = time_struct.tm_mon
day = time_struct.tm_mday
hour24 = time_struct.tm_hour
minute60 = time_struct.tm_min
second60 = time_struct.tm_sec
return [str(x) for x in [
year, month, day, hour24,
minute60, second60
]]
k = 0
for file_name in files_list:
match = prog.search(file_name)
if match:
k += 1
if match.group(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))
else:
year = int(match.group(8))
month = int(match.group(9))
day = int(match.group(10))
hour24 = 0
minute60 = 0
second60 = 0
mtime = time.mktime(datetime.datetime(
year, month, day,
hour24, minute60, second60
).timetuple())
atime = time.mktime(datetime.datetime(
year, month, day,
hour24, minute60, second60
).timetuple())
current_file = os.path.abspath(file_name)
current_time = time.localtime(
os.path.getmtime(current_file)
)
if month != current_time.tm_mon or \
year != current_time.tm_year or \
day != current_time.tm_mday:
print(
file_name + '\n' +
'size\t' + str(os.path.getsize(
current_file)/1.0e3) + ' kB' + '\n' +
' current time\t' +
str(timestruct_to_str(current_time)) + '\n' +
' actual time\t' +
str(timestruct_to_str(time.localtime(atime)))
)
os.utime(current_file, (atime, mtime))
#print('creating file ' + str(k) + ': ' + \
# os.path.abspath(current_file) + '; ' + str(atime))
print(' new time\t' + str(
timestruct_to_str(
time.localtime(
os.path.getmtime(current_file)
)
)
))
print('\n'*2)
print('total ' + str(k) + ' files changed')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment