Delete with condition : Utilities « File Directory « Ruby






Delete with condition


def delete_if(dir)
  save = Dir.getwd
  Dir.chdir(dir)
  Dir.foreach(".") do |entry|
    next if File.stat(entry).directory?
    if yield entry
      File.unlink(entry)
    end
  end
  Dir.chdir(save)
end

delete_if("/tmp") { |f| File.size(f) > 3000 }

 








Related examples in the same category

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