zoukankan      html  css  js  c++  java
  • 【Ruby】删除旧文件

    一个删除旧文件的函数,通常用于删除较老的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
     

      

  • 相关阅读:
    1,巡检脚本pexpect
    Cisco胖AP配置上网
    阿里云服务器更换密钥后,无论以何种远程连接方式都连接不上
    [PAT乙级题解]——A+B和C
    研究ThreadLocal类
    Java的反射机制
    volatile浅析
    Java 对称数据加密AES
    Java使用非对称数据加密RSA
    练习-登陆接口
  • 原文地址:https://www.cnblogs.com/yyw84/p/2085358.html
Copyright © 2011-2022 走看看