一个删除旧文件的函数,通常用于删除较老的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
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步