#!/usr/bin/ruby -w #author: caoqing #date: 06-04-2014 require 'fileutils' # target directory d = "D:/test" # size of file, MB quota = 4 * 1024 * 1024 dir = Dir.new("#{d}") entries = dir.entries # delete "." and ".." entries.delete_if { |entry| entry =~ /^\./} # convert the relative path to the full path entries.map! { |entry| File.join(dir.path, entry) } # maintain only the type of file entries.delete_if { |entry| !File.file?(entry) } # p entries # calculate the file size total_size = entries.inject(0) { |total, entry| total + File.size(entry)} # p total_size # p quota # p total_size.class # delete file if condition is satisfied FileUtils.rm_rf(Dir.glob("#{d}/*")) if total_size > quot