Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive listing of all files matching a certain filetype in Groovy

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?

like image 547
shikarishambu Avatar asked Sep 07 '10 19:09

shikarishambu


People also ask

How do I search for files in Groovy?

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 are groovy files?

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.


2 Answers

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.

like image 89
xlson Avatar answered Oct 12 '22 01:10

xlson


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 } 
like image 30
Toumi Avatar answered Oct 12 '22 01:10

Toumi