26 Nov 2022 - Tobias Klöpper
Yesterday I scrolled lazily through my phone and I was looking at the pictures gallery app. Usually the pictures are sorted by their creation date. But there are some pictures with a date, that matches the time I bought a new phone in 2021. Because of manually copying over the pictures their original creation time was reset. This annoyed me so much yesterday that I got to work on resetting their original date.
Most of the pictures were taken by my phone camera or were sent to me via a messenger app. Since their filename matches their creation time, this should not be a difficult tasks to write a little program for resetting the creation time. The fileformat was either
20201212-Time. Therefore the creation time was easy to parse as a Date.
My pictures are stored on Windows, so I first looked into Powershell. After a quick google search, I stumbled upon a guide to set the creation time via a simple Powershell script.
One step done but the creation data had to be stripped from the filenname. Since I do not know much about Powershell, I turned to my good old friend: Ruby.
Looking at the docs for the file object in Ruby was my first idea. There I found not only the methods I needed to get the file entries in the current folder but also the
File.utime method. This method sets the modification time and file changed time to whatever date value you parse as an parameter. RubyDocs/File
At last the filename was stripped via the rubys built in method
String.split. Including the splitting parameter
"_" got me the date. Now I need to parse this date through the
Time.parse(date) function and insert this value as a parameter to the
File.utime function. Et viola! My pictures now include the correct date.