Filter Project.files() method


(Paolo Finamore) #1

Hi Everyone,

I have a task in which I create a FileCollection with the Project.files() method :

FileCollection schemaFiles = files { dir.listFiles() }

Now i would like to create the same collection filtering the files() method (or the listFiles method). The desired filter should exclude files whose name is not contained in a list of file names. I want to pass this list(collection) as parameter to the task. For example if directory contains following files:

    dir---|
          |---- file1
          |---- file2
          |---- file3
          |---- file4
          |---- file5

i would be able to get a collection with just a subset of files, passing to task a list of file names e.g. ['file1','file2','file4'].
Is it possible to obtain this without loop through the files collection?

Thanks in advance
Paolo


(Lance Java) #2

I tend to find the FileTree interface more useful than FileCollection for filtering.

eg:

FileTree filtered = fileTree(dir).matching {
   include "**/file1" // will include files in nested folders
   include "file2", "file4" // files in root folder
}
Set<File> files = filtered.files

@see PatternFilterable