Hi, I have one minor issue. I''m trying to check whether a directory is empty given some path to the directory. My code does the following: if(File.directory?(dirpath)) # dirpath = public/images/1 if(File.zero?(dirpath)) FileUtils.rm_rf(dirpath) end end The problem is, while testing this, if I put a file in the directory and run the above code, it still ends up deleting the entire directory which doesn''t make sense since File.zero? should not return true. I even tried using File.size?(dirpath) but that doesn''t work either. Any help would be appreciated. Thanks, Saureen. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060606/a781bd1c/attachment.html
try something like this instead (in irb) if File.directory?(dirname) begin # try to delete the directory # Dir.delete raises a SystemCallError if the named directory is not empty Dir.delete(dirpath) rescue SystemCallError => e # handle the exception here (ie, directory was not deleted because it was not empty) puts e end else puts "#{dirname} is not a directory" end On 6/6/06, Sam Donaldson <samonderous@gmail.com> wrote:> > Hi, > > I have one minor issue. I''m trying to check whether a directory is empty > given some path to the directory. > > My code does the following: > > if(File.directory?(dirpath)) # dirpath = public/images/1 > if(File.zero?(dirpath)) > FileUtils.rm_rf(dirpath) > end > end > > The problem is, while testing this, if I put a file in the directory and > run the above code, it still ends up deleting the entire directory which > doesn''t make sense since File.zero? should not return true. I even tried > using File.size?(dirpath) but that doesn''t work either. > > Any help would be appreciated. > > Thanks, > > Saureen. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060606/fe4c8675/attachment.html
Sam Donaldson wrote:> Hi, > > I have one minor issue. I''m trying to check whether a directory is > empty > given some path to the directory. > > My code does the following: > > if(File.directory?(dirpath)) # dirpath = public/images/1 > if(File.zero?(dirpath)) > FileUtils.rm_rf(dirpath) > end > end > > The problem is, while testing this, if I put a file in the directory and > run > the above code, it still ends up deleting the entire directory which > doesn''t > make sense since File.zero? should not return true. I even tried using > File.size?(dirpath) but that doesn''t work either. > > Any help would be appreciated.File.zero? will just tell you if you have a zero-length file, which a directory is not. You could say if Dir.entries(dirpath).size < 2 # two entries for "." and ".." What I''d do, though is: require ''pathname'' if Pathname.new(dirpath).children.size == 0 I''ve already spent enough time struggling with File and Dir; Pathname makes things like this a lot easier:-) --Al Evans -- Posted via http://www.ruby-forum.com/.
Dir["*"].empty?> -------- Original-Nachricht -------- > Datum: Tue, 6 Jun 2006 16:57:21 +0200 > Von: Al Evans <anejr@alevans.com> > An: rails@lists.rubyonrails.org > Betreff: [Rails] Re: Checking if a directory is empty using File > > Sam Donaldson wrote: > > Hi, > > > > I have one minor issue. I''m trying to check whether a directory is > > empty > > given some path to the directory. > > > > My code does the following: > > > > if(File.directory?(dirpath)) # dirpath = public/images/1 > > if(File.zero?(dirpath)) > > FileUtils.rm_rf(dirpath) > > end > > end > > > > The problem is, while testing this, if I put a file in the directory and > > run > > the above code, it still ends up deleting the entire directory which > > doesn''t > > make sense since File.zero? should not return true. I even tried using > > File.size?(dirpath) but that doesn''t work either. > > > > Any help would be appreciated. > > File.zero? will just tell you if you have a zero-length file, which a > directory is not. > > You could say > > if Dir.entries(dirpath).size < 2 # two entries for "." and ".." > > What I''d do, though is: > > require ''pathname'' > if Pathname.new(dirpath).children.size == 0 > > I''ve already spent enough time struggling with File and Dir; Pathname > makes things like this a lot easier:-) > > --Al Evans > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Why not simply use that expression? Dir["*"].empty? Al Evans schrieb:> Sam Donaldson wrote: > >> Hi, >> >> I have one minor issue. I''m trying to check whether a directory is >> empty >> given some path to the directory. >> >> My code does the following: >> >> if(File.directory?(dirpath)) # dirpath = public/images/1 >> if(File.zero?(dirpath)) >> FileUtils.rm_rf(dirpath) >> end >> end >> >> The problem is, while testing this, if I put a file in the directory and >> run >> the above code, it still ends up deleting the entire directory which >> doesn''t >> make sense since File.zero? should not return true. I even tried using >> File.size?(dirpath) but that doesn''t work either. >> >> Any help would be appreciated. >> > > File.zero? will just tell you if you have a zero-length file, which a > directory is not. > > You could say > > if Dir.entries(dirpath).size < 2 # two entries for "." and ".." > > What I''d do, though is: > > require ''pathname'' > if Pathname.new(dirpath).children.size == 0 > > I''ve already spent enough time struggling with File and Dir; Pathname > makes things like this a lot easier:-) > > --Al Evans > >
Possibly Parallel Threads
- aaf and dynamic attrs: a bug?
- [ win32utils-Bugs-14360 ] Bad interaction between win32-file-stat and FileUtils
- Add the ability to ''eval'' ruby code in exec type?
- [LLVMdev] [patch] gccld not properly constructing paths when checking for bytecode
- Simple FileUtils question