开发者

Delete hidden files in Ruby

开发者 https://www.devze.com 2022-12-08 11:45 出处:网络
Does anyone know how to delete all files in a directory with Ruby. My script works well when there are no hidden files but when there are (i.e 开发者_如何转开发.svn files) I cannot delete them and Rub

Does anyone know how to delete all files in a directory with Ruby. My script works well when there are no hidden files but when there are (i.e 开发者_如何转开发.svn files) I cannot delete them and Ruby raises the Errno::ENOTEMPTY error. How do I do that ?


If you specifically want to get rid of your svn files, here's a script that will do it without harming the rest of your files:

require 'fileutils'
directories = Dir.glob(File.join('**','.svn'))
directories.each do |dir|
    FileUtils.rm_rf dir
end

Just run the script in your base svn directory and that's all there is to it (if you're on windows with the asp.net hack, just change .svn to _svn).

Regardless, look up Dir.glob; it should help you in your quest.


.svn isn't a file, it's a directory.

Check out remove_dir in FileUtils.


It probably has nothing to do with the fact, that .svn is hidden. The error suggest, that you are trying to delete a non empty directory. You need to delete all files within the directory before you can delete the directory.


Yes, you can delete (hiden) directory using FileUtils.remove_dir path.

I happend to just write a script to delete all the .svn file in the directory recursively. Hope it helps.

#!/usr/bin/ruby
require 'fileutils'
def svnC dir

    d = Dir.new(dir)
    d.each do |f|
            next if f.eql?(".") or f.eql?("..")
            #if f is directory , call svnC on it
            path = dir + "/" + "#{f}"
            if File.stat(path).directory?
                    if  f.eql?(".svn")
                            FileUtils.remove_dir path
                    else
                            svnC path
                    end
            end
      end

 end

 svnC FileUtils.pwd 


As @evan said you can do

require 'fileutils'
Dir.glob('**/.svn').each {|dir| FileUtils.rm_rf(dir) }

or you can make it a one liner and just execute it from the command line

ruby -e "require 'fileutils'; Dir.glob('**/.svn').each {|dir| FileUtils.rm_rf(dir) }"
0

精彩评论

暂无评论...
验证码 换一张
取 消