In this recipe, we will show how easy it is to filter out files and folders during a search operation on the file system.
Let's assume that we are searching for files in a current working directory. For that, we need to define a variable of the java.io.File
type that points to the .
path:
def currentDir = new File('.')
As we already mentioned in the Walking through a directory recursively recipe, Groovy adds a powerful traverse
method for helping us with our searching task. Let's explore the options that are given by that method:
traverse
method:currentDir.traverse { File file -> if (file.name.endsWith('.groovy')) { println file.name } }
traverse
method has another overloaded version that accepts a Map
of the parameters. One of those parameters is nameFilter
, which should be of the java.util.regex.Pattern
type. So, our initial code snippet can be written in the following way:currentDir.traverse(nameFilter: ~/.*.groovy/) { File file -> println file.name }
traverse
method has even more tricks up its sleeve. By specifying the type of the entries (you need to import the groovy.io.FileType
class beforehand), we can search for the pattern of the names we want to exclude (excludeNameFilter
):import static groovy.io.FileType.* ... currentDir.traverse( type: FILES, nameFilter: ~/.*.groovy/, excludeNameFilter: ~/^C.*$/) { File file -> println file.name }
visit
parameter in the map
:currentDir.traverse( type: FILES, nameFilter: { it.matches(/.*.groovy/) }, excludeNameFilter: { it.matches(/^C.*$/) }, visit: { println it.name } )
filter
/excludeFilter
closure parameters, which are given on java.io.File
objects:def today = new Date() currentDir.traverse( filter: { it.lastModified() < (today-5).time && it.name.endsWith('.groovy') }, excludeFilter: { it.isDirectory() }, visit: { println it.name } )
preDir
(or postDir
) closure parameters:import static groovy.io.FileVisitResult.* ... currentDir.traverse( preDir: { if (it.name == '.svn') { return SKIP_SUBTREE } }, nameFilter: { it.matches(/.*.groovy/) }, excludeNameFilter: { it.matches(/^C.*$/) }, visit: { println it.name } )
As you can guess, the first code snippet will walk through all the subdirectories and print all Groovy (*.groovy
) source file names that are found.
In the Searching strings with regular expressions recipe in Chapter 3, Using Groovy Language Features, we mentioned the ~//
operator, which constructs a Pattern
object. That's what we use in the second example to match the *.groovy
files and print their name again.
In the third step, we add a bit more logic to the code, since we only go through normal files by specifying FileType.FILES
in the type parameter. Other possible values are FileType.ANY
and FileType.DIRECTORIES
. The search also excludes all files starting with C
with the help of a regular expression passed to the excludeNameFilter
parameter.
The fourth example is identical to the third one, except that all filtering logic is passed through named parameters in a map.
The fifth code snippet prints the names of the files having the *.groovy
extension, which are older than 5 days and which are not directories.
The last code example behaves in the same way as the third snippet, but it also excludes the .svn
directory and all subdirectories from searching. The closure passed to preDir
returns an instance of the groovy.io.FileVisitResult
enumeration, which controls the traversing behavior. Other possible enumeration values are CONTINUE
, SKIP_SIBLINGS
, and TERMINATE
. In general, preDir
and postDir
can execute any code, and it's not mandatory to return FileVisitResult
.
There are other useful options available for the traverse
method:
maxDepth
: It says how deep should we go in our search.sort
: It is a closure that sets the sorting order in which files and directories will be processed.visitRoot
: It indicates that the visit
closure should also be called for the target search directory.preRoot
: It indicates that the preDir
closure should also be called for the target search directory.postRoot
: It indicates that postDir
closure should also be called for the target search directory.If you just want to search in the current directory without visiting its subdirectories, you can use the eachFileMatch
method instead:
currentDir.eachFileMatch(~/.*.groovy/) { File file -> println file.name }
There is also an eachDirMatch
method, in case you only need to walk through the matching first-level directories.
An initial introduction to walking through directory trees was given in the Walking through a directory recursively recipe.
Also pay your attention to the following Groovydoc references:
3.145.35.194