Version 3 of Rename and redate photo-files

Updated 2006-06-26 14:47:18

HJG 2006-05-25 - Digital cameras typically produce pictures with filenames like "100_0001.jpg", "Dscn1234.jpg" or "Imgp0001.jpg".

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".

Some software that can do such renaming:

Of course, we can do that with Tcl/Tk also, here is a simple program to do just the renaming:


  # 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 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. phots 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.


See also:


Category File