-1

I have 4500 pictures and somehow messed up the file property "Created" so they all show created on the same date. Pictures were taken with an android phone but are now on a flash drive on a Windows 10 computer. A typical filename is "1209181442" which was taken Dec 9th 2018 at 14:42 (2:42pm). Some files have suffixes such as "-burst1" or "-2" or "a" which ideally would be left intact.
It looks like the REN command in PYTHON will easily do this but I'm terrible in its syntax. Do you know how to use substring to extract the parts and shuffle the year,month,day and convert?

Mark L
  • 1

2 Answers2

0

)If your naming convention is that simple and consistent, string slicing is probably the easiest way to go. More advanced approaches can be done using the datetime libraries and regex.

filename = "1209181442-foobar.jpg"
month = filename[:2]
day = filename[2:4]
yr = filename[4:6]
time = filename[6:10]

new_filename = "20" + yr + month + day + time + filename[10:]
print(filename, new_filename)
>>> 1209181442-foobar.jpg 201812091442-foobar.jpg

So a complete example would look like (assuming the pics subdirectory only contains the files you want to change):

import os

for filename in os.listdir("pics"):
    month = filename[:2]
    day = filename[2:4]
    yr = filename[4:6]
    time = filename[6:10]

    new_filename = "20" + yr + month + day + time + filename[10:]

    os.rename(os.path.join("pics", filename), os.path.join("pics", new_filename))
feik
  • 367
  • 2
  • 17
0

With the format you provided, which is month:date:year(with 20 as prefix):hour:minute. We know that each element contains two digits. I will be using the filename you have given as a placeholder for the code:

#The filename can have the suffixes you mentioned, since this code will only take the digits that show the year, month, date, and time
orgFilename = "1209181442"

#In the variables, we are taking out each two digit number and setting it to the corresponding format. This is called slicing.

#m is month.
m = orgFilename[0:2]

#d is date
d = orgFilename[2:4]

#y is year
y = orgFilename[4:6]

#h is hour
h = orgFilename[6:8]

#mi is minute
mi = orgFilename[8:10]

#The format that will be printed is year-month-date-hour-minute
print(f"Year: 20{y} Month: {m} Date: {d} Time: {h}{mi}")


ReplitUser
  • 87
  • 6