#!/usr/bin/env ruby -w require 'find' require 'fileutils' ## --------------------------------------------------- ## -- simple picture organizer ## organizes files into seperate directories ## ## -- author: Patrick Lacson ## --------------------------------------------------- def getUniq(filename) if FileTest.exist?(filename) extension = File.extname(filename) # get file extension newname = filename # initialize newname ("A".."Z").each{ |letter| puts "trying letter ... #{letter}" basename = filename.sub(/#{Regexp.escape(extension)}$/, '') newname = basename + "_" + letter + extension if !FileTest.exist?(newname) puts "found a uniq filename #{newname}" return newname end } # close of letter loop else # file does not exist return filename end # end FileText.exist?(filename) end def usage puts "usage: organizer.rb [inputdir] [outputdir] " exit(-1) end usage if ARGV.length != 2 listdir = ARGV[0] outdir = ARGV[1] total_size = 0 Find.find(listdir) do |path| if FileTest.directory?(path) if File.basename(path)[0] == ?. Find.prune # Don't look any further into this directory. else next end else fsize = FileTest.size(path) # get file size ftime = File.mtime(path) fday = ftime.mday > 9 ? ftime.mday : "0#{ftime.day}" fmonth = ftime.month > 9 ? ftime.month : "0#{ftime.month}" fyear = ftime.year #fdir = "#{fmonth}_#{fday}_#{fyear}" # better sort -- Nathan Chilton nathanchilton@yahoo.com 02/24/05 fdir = "#{fyear}#{fmonth}#{fday}" fnewdir = File.join(outdir,fdir) FileUtils.mkdir fnewdir if !FileTest.exist?(fnewdir) fnewpath = File.join(fnewdir,File.basename(path)) fnewpath = getUniq(fnewpath) FileUtils.cp(path, fnewpath, :preserve => true,:verbose => true) #FileUtils.mv(path, fnewpath, :verbose => true) #puts "copied #{path} to #{fnewpath} (#{fsize} bytes)" #puts "month #{fmonth} day #{fday} year #{fyear}" #puts "#{ftime} #{fsize} bytestt#{path}" total_size += fsize end end puts "total: #{total_size} bytes copied"