Scan directories in groovy with ant builder file s

2019-06-06 17:13发布

问题:

I want to use fileScanner of AntBuilder to go over directories.

My code looks like:

scanner = new AntBuilder().fileScanner {
    fileset(dir:sourcedir, casesensitive:false) {
        include(name:pattern)
        type(type:'dir')
    }
}

I want to loop with the scanner just on directories, for example:

for (file in scanner) {
    assert file.directory == true
}

Any idea ? Thanks!!!!!!!!

回答1:

Here's how to do it with fileScanner

scanner = new AntBuilder().fileScanner {
    fileset(dir:sourcedir, casesensitive:false) {
        include(name:pattern)
    }
}

// Just the directories
scanner.directories().each {
    println it.name
}

You could also do it with Groovy calls:

def dirs = []
new File( sourcedir ).eachDirRecurse {
    // Check the name here, obviously the Ant pattern you have probably won't work
    if( it.name ==~ pattern ) dirs << it
}

dirs.each {
    println it.name
}