惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 阿武

Moto G 通话没声音 Android 手机技巧 一个Java程序的生死旅程 精妙语录 【Windows Phone 7】【软件发布】深圳通助手 & 深圳长途汽车查询 & 深圳地铁通 [C++ Primer] Passing an array to a function by reference/pointers -- sample 京东自动抢购机 64位的处理器支持多大的内存? 将.NET Entity Framework 的 Cache模块移植到JAVA平台 做快乐的程序员 Q 语言初学者系列:(3)Lists 初级 KDB+性能分析:内存篇 Q 语言初学者系列:(2)基本数据类型 Q 语言初学者系列:(1)开门篇 熟悉的感觉 盘点自己两年来走过的路 [JAVA]你见过这样的switch吗? - 阿武 - 博客园 网站上图片"另存为" 为什么是 bmp 格式 经实验, 网线两端都接在交换机上并不会烧毁交换机
【Ruby】删除旧文件
阿武 · 2011-06-20 · via 博客园 - 阿武

一个删除旧文件的函数,通常用于删除较老的log文件。

 module FileUtils2
    ONE_DAY_SECONDS 
= 60*60*24
    
# remove the old files, return the number of files that removed.    
    def remove_old_files(dir_path, days_ago)
        count 
= 0    
        dir_path2 
= dir_path + File::ALT_SEPARATOR unless dir_path.end_with?(File::ALT_SEPARATOR)
        d 
= Dir.new dir_path2    

        now 

= Time.now
        
        d
.each  {|filename|     
            
next if filename == '.' or filename == '..'

            file_path 

= dir_path2 + filename
            
next if File.directory?(file_path)
            
            f 
= File.new(file_path)
            diff 
= now.to_i - f.mtime.to_i
            f
.close
            day 
= diff/ONE_DAY_SECONDS
            
            
next if day <= days_ago
            
            File
.delete(file_path)
            puts 
"Delete: #{file_path}." 
            count 
+= 1
        }
        
return count
    end
end