Scan directories in groovy with ant builder file s

2019-06-06 16:34发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-06-06 17:20

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
}
查看更多
登录 后发表回答