Version 6 of Rename and redate photo-files

Updated 2006-06-26 15:46:55

HJG 2006-06-25 - Digital cameras typically produce pictures, voice-memos and movies with filenames like "100_0001.jpg", "Dscn0001.wav", "Imgp0001.jpg" and "Dscn1234.mov".

For organizing a collection of photos, it is better to rename them to a filename based on a prefix and the date and time of when the picture was taken, such as "holidays_2005-12-13_14-15-16.jpg" or "gurt2005_1213_141516a.jpg". (I prefer to use as few separators as possible)

Some software that can do such renaming:

Of course, we can do that with Tcl/Tk also. Here is a simple program for renaming just the jpg-files:


  # FotoRename1.tcl - HaJo Gurt - 2006-06-25 - http://wiki.tcl.tk/16082
  #: Rename jpg-files in current directory,
  # according to date and time, as specified in fnFormat,
  #: e.g. "dscn1234.jpg" --> "gurt2006_0625_121314.jpg"

  catch {console show}
  wm withdraw .

  set dir "."
  set fnFormat "gurt%Y_%m%d_%H%M%S"

  puts "# FotoRename1:"
  foreach fname1 [lsort [glob -dir $dir "*.jpg"]] {
     file stat $fname1 stat
     array set attr [file attributes $fname1]
     set fn1 [file tail $fname1]

    if { $stat(type) == "file" } {
      set fn2 [clock format $stat(mtime) -format $fnFormat ]
      set ext ".jpg"
      append fn2 $ext
      if { [file exist $fn2] } {
        puts "# File exists: $fn2"                ;# don't overwrite existing files
      } else {
        puts [format "mv %-30s %s" $fn1 $fn2]
        file rename -- $fn1 $fn2                
      }
    }
  }
  puts "# Done."

This assumes that the modification-date of the image-files is the date and time when the picture was shot, and that we have sufficent permissions for the renaming. This program does not consider files with the same date+time (other than not renaming more than one of them), i.e. photos that were shot within the same second.

Also, it would be nice to rename files with the same base-filename, such as "Dscn1234.jpg" and "Dscn1234.wav", i.e. voice-memos that belong to a picture.

Another desirable function would be re-dating photo-files according to the date+time from their exif-info, and maybe correcting this date by some offset (e.g. when you forgot to set the camera for daylight-saving-time, or on holidays in another timezone).


See also:


Category Date and Time - Category File