Skip to content

Instantly share code, notes, and snippets.

@erlendaakre
Created October 4, 2016 10:57
Show Gist options
  • Save erlendaakre/e27066d3e78f58f075df8b4b3d74b9f8 to your computer and use it in GitHub Desktop.
Save erlendaakre/e27066d3e78f58f075df8b4b3d74b9f8 to your computer and use it in GitHub Desktop.
Recursively find all files in a directory and subdirectories
trait RecursiveFileFindBehaviour {
import java.io.File
/**
* Recursively finds all the files in the file system under a given directory
*
* @param dir the directory to find all files in
* @param filesFound always use Set.empty when calling, used internally for keeping track of discovered files
* @return A set of files from the specified directory and all subdirectories found in tree
*/
def findFiles(dir: File, filesFound: Set[File] = Set.empty): Set[File] = {
val r = dir.listFiles().partition(p => p.isDirectory)
if (r._1.nonEmpty) r._1.flatMap(findFiles(_, r._2.toSet union filesFound)).toSet else filesFound union r._2.toSet
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment