Ruby 删除仅包含特定目录的子目录
我需要删除一堆只包含其他目录和“.svn”目录的子目录。
如果你把它看成一棵树,“叶子”只包含“.svn”目录,所以应该可以删除叶子,然后后退一个级别,删除新的叶子,等等。
我认为这段代码应该这样做,但我坚持不知道要在“某事”中放入什么。
Find.find('./com/') do |path|
if File.basename(path) == 'something'
FileUtils.remove_dir(path, true)
Find.prune
end
end
有什么建议么?
I need to delete a bunch of subdirectories that only contain other directories, and ".svn" directories.
If you look at it like a tree, the "leaves" contain only ".svn" directories, so it should be possible to delete the leaves, then step back up a level, delete the new leaves, etc.
I think this code should do it, but I'm stuck on what to put in "something".
Find.find('./com/') do |path|
if File.basename(path) == 'something'
FileUtils.remove_dir(path, true)
Find.prune
end
end
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个考虑了新的叶子(条目的 sort.reverse 意味着 /a/b/.svn 在 /a/b 之前处理;因此,如果 /a/b 为空,它将被删除,并且 size<=2 是因为使用 FNM_DOTMATCH glob 将始终返回至少 2 个条目(“.”和“..”))
This one takes new leaves into account (sort.reverse for entries means that /a/b/.svn is processed before /a/b; thus if /a/b is otherwise empty, it will be removed and size<=2 is because with FNM_DOTMATCH glob will always return a minimum of 2 entries ('.' and '..'))
这可以完成工作......但是它没有考虑到它自己的运行可以创建新的叶子
This would do the job... however it doesn't take into consideration, that the it's own run could create new leaves