coloradoblue
coloradoblue

Reputation: 625

In ruby, how might I find all Files between two dates

I'm very surprised I couldn't find an answer to this question, but I need to select all files modified between Time.now and 1.day.ago. Obviously I'm not expecting an 'ago' operator when dealing with the ruby File or FileUtil classes, but if somebody showed me one I wouldn't be surprised. :)

The other way would be an array function I think...perhaps involving the '<=>' operator which I have never used

Upvotes: 1

Views: 1256

Answers (1)

Jorge Israel Pe&#241;a
Jorge Israel Pe&#241;a

Reputation: 38586

Perhaps something like this? (untested):

selected_files = Dir.glob("*.pdf").select do |file|
  mtime = File.mtime(file)

  # if in a rails environment:
  # (1.day.ago .. Time.now).cover?(mtime)

  # if not in rails environment but want to use that code do this before that line:
  # require 'active_support/all'

  # else do the math:
  # mtime > (Time.now - 86400) and mtime < Time.now
end

Upvotes: 3

Related Questions