"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