2010-10-12 35 views
1

我想找出超过x天的文件(时间和周末不计算为了计算文件的年龄)。我只需要使用平日。范围根据日期不在一个月内不起作用(红宝石)

我的脚本正在工作,但只有当范围内的日期在同一个月内。否则,范围大小为0

我通过红宝石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 

} 

任何想法我怎么可以让它工作运行脚本?

+0

应公众假期算呢? – 2010-10-12 11:02:53

+0

@Lars Haugseth:在这种情况下并不重要 – Radek 2010-10-12 22:18:15

+0

不确定发生了什么,但'puts((Date.strptime(File.mtime(db).strftime(“%Y-%m-%d”))))。 。(Date.today))。select {| d | (1..5).include?(d.wday)} .size'现在工作正常 – Radek 2010-10-27 00:51:59

回答

0

使从

  • 设置删除所有文件的最终代码['路径']
  • 比设置['days']早 - 周末天不是c ounted
  • 而不是设置[ '排除']数组

-

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 
    } 
    } 
1

找到比X天如旧文件7天

x=7 
t=Time.now 
days=t - (x * 86400) 
Dir["*.gdb"].each do |db| 
    if File.mtime(db) < days 
    puts db 
    end 
end 

要排除周末

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 
+0

@ ghostdog74:你的代码是否排除周末? – Radek 2010-10-12 01:16:21

+0

没有。它不是。但你可以添加一个check.see编辑 – ghostdog74 2010-10-12 01:24:52

+0

酷。我会尝试的。任何想法为什么我的代码不起作用? – Radek 2010-10-12 01:29:26