tags:

views:

58

answers:

1

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?

+1  A: 

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
ghostdog74
@ghostdog74: does your code exclude weekends?
Radek
no. it does not. but you can add a check.see edit
ghostdog74
cool. I'll try that. Any idea why my code doesn't work?
Radek
@ghostdog74: I am trying to understand your code and I cannot grasp how you exclude the weekends. Exclude weekends means not to count weekend in the calculation not to exclude if I run the script on weekend.
Radek
I don't get what you mean. My code gets ALL files that are more than 7 days old AND at the same time, their wday all falls in the range 1..5. That's it.
ghostdog74
I think Radek means that weekends don't count for the purpose of calculating a file's age — that is, if it's Monday and the file was last edited Friday, it's one day old, not three.
Chuck
Oh, i see. then i misinterpreted his question.
ghostdog74
@Chuck: thank you :-) I didn't know how to put it in words
Radek