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.
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.
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.
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.
What about FileUtils.rm_rf("#{dir_path}/.", secure: true)
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With