I have a gut feeling there is a better way to do this....

View: New views
9 Messages — Rating Filter:   Alert me  

I have a gut feeling there is a better way to do this....

by phil swenson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I need a function that returns if a directory has any files in it (directories don't count).  Here's my take:

def noFilesInDirectory(dir){
def file = new File(dir)
def i = 0
file.eachFileRecurse(){
    if (it.isFile()){
        i++
    }
}
return i == 0
}

I think there is a better way... what is it?

Re: I have a gut feeling there is a better way to do this....

by phil swenson-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

here's a better version:

def isDirectoryEmpty(dir){
    def result = true
    new File(dir).eachFileRecurse(){
        if (it.isFile()){
            result = false
        }
    }
    result
}

this is OK, although inefficient because it keeps iterating even though a false condition is detected.  Looks like there is no way to break out of the eachFileRecurse loop? (break doesn't work).



def dir = "/Users/pswenson/test"
println isDirectoryEmpty(dir)

On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...> wrote:
I need a function that returns if a directory has any files in it (directories don't count).  Here's my take:

def noFilesInDirectory(dir){
def file = new File(dir)
def i = 0
file.eachFileRecurse(){
    if (it.isFile()){
        i++
    }
}
return i == 0
}

I think there is a better way... what is it?


Re: Re: I have a gut feeling there is a better way to do this....

by David Clark-9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

def isDirectoryEmpty(def dir) {
  null == dir.listFiles().find { it.name != '.' && it.name != '..'; };
}

On Tuesday 08 July 2008, phil swenson wrote:

> here's a better version:
>
> def isDirectoryEmpty(dir){
>     def result = true
>     new File(dir).eachFileRecurse(){
>         if (it.isFile()){
>             result = false
>         }
>     }
>     result
> }
>
> this is OK, although inefficient because it keeps iterating even though a
> false condition is detected.  Looks like there is no way to break out of
> the eachFileRecurse loop? (break doesn't work).
>
>
>
> def dir = "/Users/pswenson/test"
> println isDirectoryEmpty(dir)
>
> On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...> wrote:
> > I need a function that returns if a directory has any files in it
> > (directories don't count).  Here's my take:
> >
> > def noFilesInDirectory(dir){
> > def file = new File(dir)
> > def i = 0
> > file.eachFileRecurse(){
> >     if (it.isFile()){
> >         i++
> >     }
> > }
> > return i == 0
> > }
> >
> > I think there is a better way... what is it?



--
David

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: I have a gut feeling there is a better way to do this....

by Erick Erickson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can try this variant of David's solution...

def isDirectoryEmpty(def dir) {
    null == dir.listFiles().find {
        it.isFile()
    }
}

println isDirectoryEmpty(new File("<insert dir path here>"))

I think David's original solution returns true if there are any
subdirs, but I'm sure didn't think of the find option until
he suggested it.

Best
Erick



On Tue, Jul 8, 2008 at 4:49 PM, David Clark <davidclark@...> wrote:
def isDirectoryEmpty(def dir) {
 null == dir.listFiles().find { it.name != '.' && it.name != '..'; };
}

On Tuesday 08 July 2008, phil swenson wrote:
> here's a better version:
>
> def isDirectoryEmpty(dir){
>     def result = true
>     new File(dir).eachFileRecurse(){
>         if (it.isFile()){
>             result = false
>         }
>     }
>     result
> }
>
> this is OK, although inefficient because it keeps iterating even though a
> false condition is detected.  Looks like there is no way to break out of
> the eachFileRecurse loop? (break doesn't work).
>
>
>
> def dir = "/Users/pswenson/test"
> println isDirectoryEmpty(dir)
>
> On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...> wrote:
> > I need a function that returns if a directory has any files in it
> > (directories don't count).  Here's my take:
> >
> > def noFilesInDirectory(dir){
> > def file = new File(dir)
> > def i = 0
> > file.eachFileRecurse(){
> >     if (it.isFile()){
> >         i++
> >     }
> > }
> > return i == 0
> > }
> >
> > I think there is a better way... what is it?



--
David

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email




Re: Re: I have a gut feeling there is a better way to do this....

by Jim White :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

David Clark wrote:

> def isDirectoryEmpty(def dir) {
>   null == dir.listFiles().find { it.name != '.' && it.name != '..'; };
> }

That isn't properly WORA and it doesn't address what I think Phil's
issue is which is that he'd like it to short-circuit.

Something like this is about as effecient as you can get:

def isDirectoryEmpty(File dir)
{
    List dirlist = [dir]

    while (dirlist) {
       File adir = dirlist.pop()
       File[] files = adir.listFiles()
       for (f in files) {
           if (f.isFile()) return false
           dirlist.push(f)
       }
    }

    return true
}

Jim

> On Tuesday 08 July 2008, phil swenson wrote:
>
>>here's a better version:
>>
>>def isDirectoryEmpty(dir){
>>    def result = true
>>    new File(dir).eachFileRecurse(){
>>        if (it.isFile()){
>>            result = false
>>        }
>>    }
>>    result
>>}
>>
>>this is OK, although inefficient because it keeps iterating even though a
>>false condition is detected.  Looks like there is no way to break out of
>>the eachFileRecurse loop? (break doesn't work).
>>
>>
>>
>>def dir = "/Users/pswenson/test"
>>println isDirectoryEmpty(dir)
>>
>>On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...> wrote:
>>
>>>I need a function that returns if a directory has any files in it
>>>(directories don't count).  Here's my take:
>>>
>>>def noFilesInDirectory(dir){
>>>def file = new File(dir)
>>>def i = 0
>>>file.eachFileRecurse(){
>>>    if (it.isFile()){
>>>        i++
>>>    }
>>>}
>>>return i == 0
>>>}
>>>
>>>I think there is a better way... what is it?
>
>
>
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: I have a gut feeling there is a better way to do this....

by David Clark-9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"find" does short circuit, that's why I used it.

It isn't WORA, but it works on *NIX and I am pretty sure it works on Windows.

On Tuesday 08 July 2008, Jim White wrote:

> David Clark wrote:
> > def isDirectoryEmpty(def dir) {
> >   null == dir.listFiles().find { it.name != '.' && it.name != '..'; };
> > }
>
> That isn't properly WORA and it doesn't address what I think Phil's
> issue is which is that he'd like it to short-circuit.
>
> Something like this is about as effecient as you can get:
>
> def isDirectoryEmpty(File dir)
> {
>     List dirlist = [dir]
>
>     while (dirlist) {
>        File adir = dirlist.pop()
>        File[] files = adir.listFiles()
>        for (f in files) {
>            if (f.isFile()) return false
>            dirlist.push(f)
>        }
>     }
>
>     return true
> }
>
> Jim
>
> > On Tuesday 08 July 2008, phil swenson wrote:
> >>here's a better version:
> >>
> >>def isDirectoryEmpty(dir){
> >>    def result = true
> >>    new File(dir).eachFileRecurse(){
> >>        if (it.isFile()){
> >>            result = false
> >>        }
> >>    }
> >>    result
> >>}
> >>
> >>this is OK, although inefficient because it keeps iterating even though a
> >>false condition is detected.  Looks like there is no way to break out of
> >>the eachFileRecurse loop? (break doesn't work).
> >>
> >>
> >>
> >>def dir = "/Users/pswenson/test"
> >>println isDirectoryEmpty(dir)
> >>
> >>On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...>
wrote:

> >>>I need a function that returns if a directory has any files in it
> >>>(directories don't count).  Here's my take:
> >>>
> >>>def noFilesInDirectory(dir){
> >>>def file = new File(dir)
> >>>def i = 0
> >>>file.eachFileRecurse(){
> >>>    if (it.isFile()){
> >>>        i++
> >>>    }
> >>>}
> >>>return i == 0
> >>>}
> >>>
> >>>I think there is a better way... what is it?
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email



--
David

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Re: I have a gut feeling there is a better way to do this....

by frapas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

With an Exception ...
 
 
def isDirectoryEmpty(dir){
    try{
    new File(dir).eachFileRecurse(){
        if(it.isFile()) throw( new Exception())
        }
    }catch(e){return false}
    return true;
}


Francesco

On Tue, Jul 8, 2008 at 10:38 PM, phil swenson <phil@...> wrote:
here's a better version:

def isDirectoryEmpty(dir){
    def result = true
    new File(dir).eachFileRecurse(){
        if (it.isFile()){
            result = false
        }
    }
    result
}

this is OK, although inefficient because it keeps iterating even though a false condition is detected.  Looks like there is no way to break out of the eachFileRecurse loop? (break doesn't work).



def dir = "/Users/pswenson/test"
println isDirectoryEmpty(dir)


On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...> wrote:
I need a function that returns if a directory has any files in it (directories don't count).  Here's my take:

def noFilesInDirectory(dir){
def file = new File(dir)
def i = 0
file.eachFileRecurse(){
    if (it.isFile()){
        i++
    }
}
return i == 0
}

I think there is a better way... what is it?



Re: Re: I have a gut feeling there is a better way to do this....

by Erick Erickson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

But throwing an exception to terminate a loop is...er...ugly, almost
makes me want a GOTO <G>.

The snippet I posted actually does short-circuit, I added a println in
the closure and played with it a bit, so David's absolutely correct
about that.

And no, the original didn't work on a windows machine when I tested it. It
makes no distinction between files and directories, so it returns "true" if
there's a directory with *only* a directory below it. But the problem statement
is to return true if there are files, directories don't count.

But it doesn't do very well if recursion is actually required, dont' know
what to do there.

Best
Erick

On Tue, Jul 8, 2008 at 5:53 PM, Francesco Pasqualini <frapas@...> wrote:
With an Exception ...
 
 
def isDirectoryEmpty(dir){
    try{
    new File(dir).eachFileRecurse(){
        if(it.isFile()) throw( new Exception())
        }
    }catch(e){return false}
    return true;
}


Francesco

On Tue, Jul 8, 2008 at 10:38 PM, phil swenson <phil@...> wrote:
here's a better version:

def isDirectoryEmpty(dir){
    def result = true
    new File(dir).eachFileRecurse(){
        if (it.isFile()){
            result = false
        }
    }
    result
}

this is OK, although inefficient because it keeps iterating even though a false condition is detected.  Looks like there is no way to break out of the eachFileRecurse loop? (break doesn't work).



def dir = "/Users/pswenson/test"
println isDirectoryEmpty(dir)


On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...> wrote:
I need a function that returns if a directory has any files in it (directories don't count).  Here's my take:

def noFilesInDirectory(dir){
def file = new File(dir)
def i = 0
file.eachFileRecurse(){
    if (it.isFile()){
        i++
    }
}
return i == 0
}

I think there is a better way... what is it?




Re: Re: I have a gut feeling there is a better way to do this....

by David Clark-9 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sorry, I didn't see the sub directories don't count part of the specification.
I thought isDirectoryEmpty meant look for an empty directory.  You might want
to change the function name to make it a bit clearer to those lazy folks,
like myself, who just look at function names.

On Tuesday 08 July 2008, Erick Erickson wrote:

> But throwing an exception to terminate a loop is...er...ugly, almost
> makes me want a GOTO <G>.
>
> The snippet I posted actually does short-circuit, I added a println in
> the closure and played with it a bit, so David's absolutely correct
> about that.
>
> And no, the original didn't work on a windows machine when I tested it. It
> makes no distinction between files and directories, so it returns "true" if
> there's a directory with *only* a directory below it. But the problem
> statement
> is to return true if there are files, directories don't count.
>
> But it doesn't do very well if recursion is actually required, dont' know
> what to do there.
>
> Best
> Erick
>
> On Tue, Jul 8, 2008 at 5:53 PM, Francesco Pasqualini <frapas@...>
>
> wrote:
> > With an Exception ...
> >
> >
> > def isDirectoryEmpty(dir){
> >     try{
> >     new File(dir).eachFileRecurse(){
> >         if(it.isFile()) throw( new Exception())
> >         }
> >     }catch(e){return false}
> >     return true;
> > }
> >
> >
> > Francesco
> > On Tue, Jul 8, 2008 at 10:38 PM, phil swenson <phil@...>
> >
> > wrote:
> >> here's a better version:
> >>
> >> def isDirectoryEmpty(dir){
> >>     def result = true
> >>     new File(dir).eachFileRecurse(){
> >>         if (it.isFile()){
> >>             result = false
> >>         }
> >>     }
> >>     result
> >> }
> >>
> >> this is OK, although inefficient because it keeps iterating even though
> >> a false condition is detected.  Looks like there is no way to break out
> >> of the eachFileRecurse loop? (break doesn't work).
> >>
> >>
> >>
> >> def dir = "/Users/pswenson/test"
> >> println isDirectoryEmpty(dir)
> >>
> >>
> >> On Tue, Jul 8, 2008 at 2:20 PM, phil swenson <phil.swenson@...>
> >>
> >> wrote:
> >>> I need a function that returns if a directory has any files in it
> >>> (directories don't count).  Here's my take:
> >>>
> >>> def noFilesInDirectory(dir){
> >>> def file = new File(dir)
> >>> def i = 0
> >>> file.eachFileRecurse(){
> >>>     if (it.isFile()){
> >>>         i++
> >>>     }
> >>> }
> >>> return i == 0
> >>> }
> >>>
> >>> I think there is a better way... what is it?



--
David

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price