Delete folder older than a certain time : Utilities « File Directory « Ruby






Delete folder older than a certain time


def delete_older(dir, time)
  save = Dir.getwd
  Dir.chdir(dir)
  Dir.foreach(".") do |entry|
    # We're not handling directories here
    next if File.stat(entry).directory?
    # Use the modification time
    if File.mtime(entry) < time
      File.unlink(entry)
    end
  end
  Dir.chdir(save)
end

delete_older("/tmp",Time.local(2001,3,29,18,38,0))

 








Related examples in the same category

1.delete a folder recursively
2.Find files
3.Delete with condition
4.Get free disk space under unix with df command
5.Get free disk space under windows with dir command
6.Call system function to delete a folder
7.Showing Line Numbers
8.Parse line: value|value
9.Change a | delimited file