Ruby 删除仅包含特定目录的子目录

发布于 2024-07-16 20:53:29 字数 332 浏览 5 评论 0原文

我需要删除一堆只包含其他目录和“.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2024-07-23 20:53:29

这个考虑了新的叶子(条目的 sort.reverse 意味着 /a/b/.svn 在 /a/b 之前处理;因此,如果 /a/b 为空,它将被删除,并且 size<=2 是因为使用 FNM_DOTMATCH glob 将始终返回至少 2 个条目(“.”和“..”))

require 'fileutils'

def delete_leaves(dirname)
    Dir.glob(dirname+"/**/",File::FNM_DOTMATCH).sort.reverse.each do |d|
        FileUtils.rm_rf(d) if d.match(/.svn/) or Dir.glob(d+"/*",File::FNM_DOTMATCH).size<=2  
    end  
end
delete_leaves(ARGV[0])

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 '..'))

require 'fileutils'

def delete_leaves(dirname)
    Dir.glob(dirname+"/**/",File::FNM_DOTMATCH).sort.reverse.each do |d|
        FileUtils.rm_rf(d) if d.match(/.svn/) or Dir.glob(d+"/*",File::FNM_DOTMATCH).size<=2  
    end  
end
delete_leaves(ARGV[0])
初见终念 2024-07-23 20:53:29

这可以完成工作......但是它没有考虑到它自己的运行可以创建新的叶子

#!/usr/bin/env ruby

require 'fileutils'

def remove_leaves(dir=".")
  Dir.chdir(dir) do 
    entries=Dir.entries(Dir.pwd).reject { |e| e=="." or e==".."}
    if entries.size == 1 and entries.first == ".svn"
      puts "Removing #{Dir.pwd}"
      FileUtils.rm_rf(Dir.pwd)
    else
      entries.each do |e|
        if File.directory? e
          remove_leaves(e)
        end
      end
    end
  end
end

remove_leaves

This would do the job... however it doesn't take into consideration, that the it's own run could create new leaves

#!/usr/bin/env ruby

require 'fileutils'

def remove_leaves(dir=".")
  Dir.chdir(dir) do 
    entries=Dir.entries(Dir.pwd).reject { |e| e=="." or e==".."}
    if entries.size == 1 and entries.first == ".svn"
      puts "Removing #{Dir.pwd}"
      FileUtils.rm_rf(Dir.pwd)
    else
      entries.each do |e|
        if File.directory? e
          remove_leaves(e)
        end
      end
    end
  end
end

remove_leaves
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文