Scan directories in groovy with ant builder file scanner

流过昼夜 提交于 2019-12-08 05:43:40

问题


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
}


来源:https://stackoverflow.com/questions/17206150/scan-directories-in-groovy-with-ant-builder-file-scanner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!