Photo Sorter

This is a simple this is a script for sorting images and camera files into folders per date. I used to use the software that came with my second digital camera, Canon ZoomBrowser, to do this, but as it was the only thing I used the software for, I though a wee Tcl script made a lot more sense.

It creates the folders it needs as it goes. For this is uses YYYY_MM_DD, as in 2011_06_22.

The whole code is just pasted in below, it's got a few comments in, but any bits that don't make sense, just add a question.

    cd "C:/docs/My Pictures/"
    puts "photo sorter"

    # glob for searching
    set file_list [glob \
    "to_sort/*.jpg" \
    "to_sort/*.avi" \
    "to_sort/*.jpeg" \
    "to_sort/*.3gp" \
    "to_sort/*/*.jpg" \
    "to_sort/*/*.avi" \
    "to_sort/*/*.jpeg" \
    "to_sort/*/*.3gp" \
    "to_sort/*/*/*.jpg" \
    "to_sort/*/*/*.avi" \
    "to_sort/*/*/*.jpeg" \
    "to_sort/*/*/*.3gp" \
    "to_sort/*/*/*/*.jpg" \
    "to_sort/*/*/*/*.avi" \
    "to_sort/*/*/*/*.jpeg" \
    "to_sort/*/*/*/*.3gp" \
    "to_sort/*/*/*/*/*.jpg" \
    "to_sort/*/*/*/*/*.avi" \
    "to_sort/*/*/*/*/*.jpeg" \
    "to_sort/*/*/*/*/*.3gp" \
    ]

    # utility vars
    set filecount 0
    set newdircount 0

    #loo
    foreach myfile $file_list {
        puts $myfile
        puts [file stat $myfile ctime] 
        set file_time [file mtime $myfile]
        puts [ clock format $file_time ]
        set file_year [ clock format $file_time -format %Y ]
        set file_month [ clock format $file_time -format %m ]
        set file_day [ clock format $file_time -format %d ]
        puts $file_year
        puts $file_month
        puts $file_day
        
        set folder_name [ clock format $file_time -format %Y ]_[ clock format $file_time -format %m ]_[ clock format $file_time -format %d ]
        puts $folder_name
        
        puts "check it exists"
        if { [file isdirectory $folder_name] == 0 } {
            puts "no it doesn't, so we'll make it"
            file mkdir $folder_name
            
            incr newdircount
        }
        
        
        puts "move $myfile to $folder_name"
        file rename $myfile $folder_name 
        
        incr filecount
        
        
    }

    puts "copied $filecount files"
    puts "created $newdircount new directories"