From: ara.t.howard@... Date: 2006-10-16T23:19:30+09:00 Subject: Re: Test if Directory is Empty On Mon, 16 Oct 2006, Daniel Berger wrote: > lroland@gmail.com wrote: >> Hi all >> >> I browsed through the FileUtils and Dir documentation and found no >> obvious way to test if a directory exists and is empty. Various hacks >> can be used (like checking file count and size, testing if the >> directory can be deleted..) but they all left me wondering if i some >> how missed a Dir.isEmpty? like method ? >> >> So in short is there a real Ruby way of telling whether a directory if >> empty > > if Dir["/foo/bar/*"].empty? that'll crawl if a directory is huge though... i've used this d = "the_dir" empty = catch("empty"){ Dir.glob("#{ d }/*"){ throw "empty", false } # if we find any file return false Dir.glob("#{ d }/.*"){ throw "empty", false } # if we find any dotfile/hidden return false throw "empty", true # otherwise return true } which someone will probably compact... note that it returns false when the first file/dotfile is found. cheers. -a -- my religion is very simple. my religion is kindness. -- the dalai lama