Skip to content

Instantly share code, notes, and snippets.

@kdabir
Last active December 6, 2022 08:12
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kdabir/7181617 to your computer and use it in GitHub Desktop.
Save kdabir/7181617 to your computer and use it in GitHub Desktop.
A groovy directory traverse that excludes given dirs. Change the last closure to do something interesting instead of printing the dir name
import groovy.io.FileType
import groovy.io.FileVisitResult
final excludedDirs = ['.svn', '.git', '.hg', '.idea', 'node_modules', '.gradle', 'build']
int count = 0
new File(root).traverse(
type : FileType.DIRECTORIES,
preDir : { if (it.name in excludedDirs) return FileVisitResult.SKIP_SUBTREE }, // excludes children of excluded dirs
excludeNameFilter : { it in excludedDirs }, // excludes the excluded dirs as well
// nameFilter : { it == 'settings.gradle'} // matched only given names
) {println it; count++}
println "count:$count"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment