Renaming Photos by Timestamp

Posted on October 18, 2016 at 16:15 Java No Comments

Including my cellphone, I used to have three cameras (used because my waterproof one died on me right after my recent trip to Palawan). Most smartphones already save photos with timestamps as the filenames but as for digital cameras, as far as I know, it’s not really the case. In general, the file naming convention varies from one device to another. The lines in Java that follow are not really anything special; it just reads the Last Modified metadata and uses it as the filename. I’ve been using this for quite some time now just because I want to be able to view photos chronologically once I consolidate them from multiple devices. That simple. Okay, and a bit of an OCD, too.

String dirName = "C:/Photos/Maldives"; //in my dreams
String fileNameExt = ".jpg"; //JPEG format
String fileNamePref = "RIMG"; //just blank to rename all photos
String fileNameFmt = "yyyyMMdd_HHmmss"; //e.g. 20161018_161534 for October 18, 2016 4:15:34 PM (should be unique enough!)

File directory = new File(dirName);
File[] photos = directory.listFiles();

for(File photo : photos) {
   String filename = photo.getName();

   if(filename.toLowerCase().endsWith(fileNameExt) && filename.startsWith(fileNamePref)) {
      SimpleDateFormat sdf = new SimpleDateFormat(fileNameFmt);
      String newFilename = sdf.format(photo.lastModified()) + fileNameExt;
      File renamedPhoto = new File(photo.getParent() + "/" + newFilename);

      if(!photo.renameTo(renamedPhoto)) {
         System.out.println("Failed to rename: " + photo.getAbsolutePath());
      }
   } else {
      System.out.println("Not a " + fileNamePref + "*" + fileNameExt + ": " + photo.getAbsolutePath());
   }
}

This is under the assumption that the date and time in your devices have been set correctly. Different time zones are no longer within the scope of this post but is accommodated in—apparently someone already thought of the same—Renaming jpeg files with date+time stamp. Ideally, all photos should be renamed successfully since no two photos can be taken at exactly the same time (burst shots are another story). But with multiple cameras, there can simply be conflict sometimes. Lastly, you should be careful when copying or moving photos because sometimes the Last Modified data are changed to the date you copied or moved them. Should that be the case, the given source code no longer holds any value.

Wallowing to Undaunted

Posted on September 1, 2016 at 14:00 Life List 2 Comments

I was 26 when I started prefixing my number with an adjective, to describe what that year would be like. I find sense in it. Last year it was “wallowing”. Nasty-sounding, I know, but after reading its definition after being suggested by a friend, well, “I choose you!” happened. And that’s way before Pokémon GO. Since then, I’ve gone to places and did things month after month but never a single update in this good old hub of mine. About time.

And then I needed to select another adjective. But before that, there was the lovely Calaguas with my very first workmates for our annual summer getaway. Given its distance from the metro, it’s just not right to spend just a night there, which we didn’t do. As one of us also gets a year older on the same month, we did a double celebration a week later with other friends at Walkyrie. Such a long night. I’d consider attaching a GoPro to my head next time for me to watch everything and laugh at myself first thing in the morning. Two days before finally flipping another digit, I skydived in Bantayan and spent a memorable time in Cebu. 😎

Skydiving in Bantayan

Like most people with itchy feet, most of my wanderings are born out of seat sales, so I guess I’m saved from being labeled as a spendthrift wanderlust. As of writing I’m currently in Palawan. My tickets, bought last year, only cost 485.62 PHP. Who could resist that, right? Well, my company last year. But not me. And I look forward to blogging about this trip.

I wouldn’t say I’m getting tired of going to places, and here are the cents I’ve picked up along the way up to this very day:

  1. Plans can go haywire, with most of the group suddenly being unable to make it and then the entire trip crumbles. When planning, it could help to have a group chat with only those people who have something to say in it. Ditch those who just read and send thumbs-up signs. (Yes, there’s a bit bitterness there.)
  2. Even just once, travel alone. Just you and your own two feet. It’s liberating.
  3. Priorities change so eventually all your travel buddies will no longer be able to always go with you. Learn to accept that.
  4. Your priorities will eventually change as well so be prepared for it, because what-ifs and if-onlys are such inglorious bastards.