Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty directory (delete all files)

Tags:

ruby

What would be a safe and efficient way to delete all files in a directory in pure Ruby? I wrote

Dir.foreach(dir_path) {|f| File.delete(f) if f != '.' && f != '..'} 

but it gives me a No such file or directory error.

Thank you.

like image 214
Pablo Avatar asked Nov 22 '11 09:11

Pablo


People also ask

Which command delete empty directory?

rmdir is a command-line utility for deleting empty directories. It is useful when you want to delete a directory only if it is empty, without needing to check whether the directory is empty or not. In this case, you will need to use the rm command or manually remove the directory contents before you can delete it.

How can I delete all files in a directory without prompt?

Using the -r flag to deleting a non-empty directory. If you do not want a prompt before deleting the directory and its contents, use the -rf flag. This will remove everything inside the directory, including the directory itself, without any confirmation.

How do you delete all files in a directory windows CMD?

To delete all of the files in the current directory, press Y and then press ENTER. To cancel the deletion, press N and then press ENTER.


1 Answers

What about FileUtils.rm_rf("#{dir_path}/.", secure: true)?

like image 145
Mario Uher Avatar answered Oct 02 '22 15:10

Mario Uher