#!/usr/bin/env ruby action = 'LIST' # 'LIST' or 'DELETE' or 'TRASH' age = 14 # in days (2 weeks default) # DO NOT CHANGE ANYTHING BELOW THIS LINE require 'fileutils' def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) distance_in_minutes = (((to_time - from_time).abs)/60).round distance_in_seconds = ((to_time - from_time).abs).round case distance_in_minutes when 0..1 return (distance_in_minutes==0) ? 'less than a minute' : '1 minute' unless include_seconds case distance_in_seconds when 0..5 then 'less than 5 seconds' when 6..10 then 'less than 10 seconds' when 11..20 then 'less than 20 seconds' when 21..40 then 'half a minute' when 41..59 then 'less than a minute' else '1 minute' end when 2..45 then "#{distance_in_minutes} minutes" when 46..90 then 'about 1 hour' when 90..1440 then "about #{(distance_in_minutes.to_f / 60.0).round} hours" when 1441..2880 then '1 day' else "#{(distance_in_minutes / 1440).round} days" end end def last_accessed(dir_name) # puts 'called' best = Time.at(0) Dir.chdir(dir_name) do Dir.foreach('.') do |entry| next if entry == '.' or entry == '..' # puts entry+'!' if File.directory? entry x = last_accessed entry best = x if x > best else x = File.atime(entry) best = x if x > best end end end return best end Dir.chdir($*[0] || Dir.pwd) do Dir.foreach('.') do |entry| # puts entry next if entry == '.' or entry == '..' x = (File.directory?(entry) ? last_accessed(entry) : File.atime(entry)) if Time.now - x > 60 * 60 * 24 * age case action when 'DELETE' File.delete(entry) when 'TRASH' FileUtils.mv(entry, File.expand_path('~/Trash/')) else puts entry + ': ' + distance_of_time_in_words(x, Time.now, true) end end end end