[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Test if Directory is Empty

lroland@gmail.com

10/16/2006 10:36:00 AM

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


--
Lars Roland

14 Answers

Daniel Berger

10/16/2006 10:47:00 AM

0

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?

Regards,

Dan

Ara.T.Howard

10/16/2006 2:20:00 PM

0

Nobuyoshi Nakada

10/16/2006 4:28:00 PM

0

Hi,

At Mon, 16 Oct 2006 23:19:30 +0900,
ara.t.howard@noaa.gov wrote in [ruby-talk:220018]:
> which someone will probably compact... note that it returns false when the
> first file/dotfile is found.

!Dir.foreach(dirname) {|n| break true unless /\A\.\.?\z/ =~ n}
!Dir.enum_for(:foreach, dirname).any? {|n| /\A\.\.?\z/ !~ n}

--
Nobu Nakada

Ara.T.Howard

10/16/2006 4:43:00 PM

0

WATANABE Hirofumi

10/16/2006 4:53:00 PM

0

Hi,

Nobuyoshi Nakada <nobu@ruby-lang.org> writes:

> At Mon, 16 Oct 2006 23:19:30 +0900,
> ara.t.howard@noaa.gov wrote in [ruby-talk:220018]:
> > which someone will probably compact... note that it returns false when the
> > first file/dotfile is found.
>
> !Dir.foreach(dirname) {|n| break true unless /\A\.\.?\z/ =~ n}
> !Dir.enum_for(:foreach, dirname).any? {|n| /\A\.\.?\z/ !~ n}

Dir.entries(dirname).join == "..."

--
eban

Chris Hulan

10/16/2006 5:01:00 PM

0

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
>
>
> --
> Lars Roland

You could try:

class Dir
def Dir.isEmpty?(path)
Dir.entries(path) == [".", ".."]
end
end

Cheers
Chris

Joel VanderWerf

10/16/2006 5:18:00 PM

0

WATANABE Hirofumi wrote:
> Dir.entries(dirname).join == "..."

Dir.entries(dirname).size == 2

But this still has the disadvantage that Ara mentioned for huge dirs...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Norgg

10/16/2006 7:17:00 PM

0

ara.t.howard@noaa.gov wrote:
> 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

Not sure about using throw to return values there... Also, won't the
"Dir.glob("#{ d }/.*")" return [".", ".."] (well, pass "." to the block)
and break this?

How about:

def isEmpty?(dir)
Dir.glob("#{dir}/*", File::FNM_DOTMATCH) do |f|
return false unless f =~ /\.\.?/
end
return true
end

Daniel Berger

10/16/2006 10:35:00 PM

0


ara.t.howard@noaa.gov wrote:
> 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

<snip>

True. Oh, look - Windows has a PathIsDirectoryEmpty() function. :)

win32-dir 0.3.1 coming soon....

Thanks,

Dan

Ara.T.Howard

10/17/2006 3:54:00 AM

0