如何使用 Ruby-Rails 删除文件夹的所有内容? [英] How to delete all contents of a folder with Ruby-Rails?

查看:39
本文介绍了如何使用 Ruby-Rails 删除文件夹的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 public/cache 文件夹,里面有文件和文件夹.如何使用 rake 任务完全清空该文件夹?

I have a public/cache folder which has files and folders. How can I completely empty that folder using a rake task?

推荐答案

Ruby 在 FileUtils 模块,可用于删除文件和非空文件夹/目录:

Ruby has the *nix rm -rf equivalent in the FileUtils module that you can use to delete both files and non-empty folders/directories:

FileUtils.rm_rf('dir/to/remove')

要保留目录本身并仅删除其内容:

To keep the directory itself and only remove its contents:

FileUtils.rm_rf(Dir.glob('dir/to/remove/*'))

FileUtils.rm_rf(Dir['dir/to/remove/*'])      # shorter version of above

这篇关于如何使用 Ruby-Rails 删除文件夹的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆