#!/usr/bin/env ruby -w require 'find' require 'fileutils' ## --------------------------------------------------- ## -- simple picture organizer ## organizes files into seperate directories ## ## -- author: Patrick Lacson (placson [at] yahoo [dot] com) ## --------------------------------------------------- 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)) #FileUtils.cp(path, fnewpath, :preserve => 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"