delete a folder recursively : Utilities « File Directory « Ruby






delete a folder recursively


def delete_all(dir)
  Dir.foreach(dir) do |e|
    # Don't bother with . and ..
    next if [".",".."].include? e
    fullname = dir + File::Separator + e
    if FileTest::directory?(fullname)
      delete_all(fullname)
    else
      File.delete(fullname)
    end
  end
  Dir.delete(dir)
end


delete_all("dir1")   # Remove dir1 and everything under it!

 








Related examples in the same category

1.Find files
2.Delete with condition
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