Reputation: 11091
I want to find out files that are older than x days (time and weekends don't count for the purpose of calculating a file's age). I need to use only weekdays.
My script is working but only if the date from the range are within the same month. Otherwise the range size is 0.
I run the script via ruby 1.8.7 (2008-08-11 patchlevel 72) [x86_64-linux]
Dir['*.gdb'].each { |db|
puts db
puts ((Date.strptime(File.mtime(db).strftime("%Y-%m-%d")))..(Date.today)).select {|d| (1..5).include?(d.wday) }.size
}
any idea how I can make it work?
Upvotes: 1
Views: 245
Reputation: 11091
so the final code that deletes all files from
-
require 'date'
settings = {
'radek' => { 'path' => '/var/lib/firebird/data/radek*.gdb','days' => '3','exclude'=>['radek_rft.gdb','radek_tmp.gdb','radek_test.gdb','radek_cmapping.gdb'] }
}
settings.each_value { |user|
user['exclude'].collect! {|file| file.downcase }
Dir[user['path']].each { |db|
old = ( (Date.strptime(File.mtime(db).strftime("%Y-%m-%d")))..(Date.today)).select {|d| (1..5).include?(d.wday) }.size - 1
if (old.to_i >= user['days'].to_i) and not(user['exclude'].include?(File.basename(db))) then output= %x[rm #{db}] end
}
}
Upvotes: 0
Reputation: 342263
To find files older than X days eg 7 days
x=7
t=Time.now
days=t - (x * 86400)
Dir["*.gdb"].each do |db|
if File.mtime(db) < days
puts db
end
end
To exclude weekends
t=Time.now # get current date
days=t - (7 * 86400) # get date 7 days before
Dir["*.gdb"].each do |db|
wd=File.mtime(db).wday # get the wday of file. 1 (monday), ... 5 (friday)
if File.mtime(db) < days and wd.between?(1,5)
# File.mtime(db) < days means get files older than 7 days
# at the same time check the wday of the file whether they are in 1..5 range
# using wd.between?(1,5)
puts db
end
end
Upvotes: 1