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. Is there a way to modify this to list files in the root folder? Or, is there a different way to do it?
fileFind.groovy The above script will print out the name of any file it finds matching the provided string. If I pass $ to it without escaping the dollar sign, all files in the directory are returned. Using backslash to escape the dollar sign accomplishes what I want (only files with $ in their name).
What is a GROOVY file? A GROOVY file is a source code file written in the Groovy programming language. Code written inside a GROOVY file is similar to the object-oriented language Java, that makes it easy for design and development of applications.
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.
groovy version 2.4.7 :
new File(pathToFolder).traverse(type: groovy.io.FileType.FILES) { it -> println it }
you can also add filter like
new File(parentPath).traverse(type: groovy.io.FileType.FILES, nameFilter: ~/patternRegex/) { it -> println it }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With