Ruby 怎样得到一个目录下所有特定日期创建的文件

small_fish__ · January 16, 2013 · Last by small_fish__ replied at January 17, 2013 · 1987 hits

问题如题,找 ruby 自带的方法很久没有找到,于是自己写了一个,但是感觉效率很不高。代码如下

all_log_path = Dir["*.log"]
query_date =  ARGV[0]
query_date ||= Time.now.to_s
query_date = Date.parse(query_date)

files = []
all_log_path.each do |log_path|
   file_modify_date = Date.parse((`stat #{log_path} |grep Modify`).to_s.split(/\s+/)[1])
   files << log_path  if query_date == file_modify_date
end

求大神指点简单方法~

require 'find'
require 'active_support/core_ext'
files =  Find.find('.').select {|f| File.ctime(f).today? }

抛砖引玉~

#1 楼 @qhwa 一直在找一个 File.ctime(f) 这样的方法 不过貌似也可以~~Dir.glob('*').select {|f| File.ctime(f) xxxx}

You need to Sign in before reply, if you don't have an account, please Sign up first.