Recursive listing of all files matching a certain filetype in Groovy

前端 未结 4 440
暖寄归人
暖寄归人 2020-12-28 12:39

I am trying to recursively list all files that match a particular file type in Groovy. This example almost does it. However, it does not list the files in the root folder. I

4条回答
  •  萌比男神i
    2020-12-28 13:20

    This should solve your problem:

    import static groovy.io.FileType.FILES
    
    new File('.').eachFileRecurse(FILES) {
        if(it.name.endsWith('.groovy')) {
            println it
        }
    }
    

    eachFileRecurse takes an enum FileType that specifies that you are only interested in files. The rest of the problem is easily solved by filtering on the name of the file. Might be worth mentioning that eachFileRecurse normally recurses over both files and folders while eachDirRecurse only finds folders.

提交回复
热议问题