Skip to content

Instantly share code, notes, and snippets.

@keitaoouchi
Created April 5, 2012 02:39
Show Gist options
  • Save keitaoouchi/2307587 to your computer and use it in GitHub Desktop.
Save keitaoouchi/2307587 to your computer and use it in GitHub Desktop.
Recursive directory listings with ignore pattern.
import scala.collection.mutable.Queue
import scala.collection.mutable.ListBuffer
object RecursiveListing {
def main(args: Array[String]) {
val result = listing("/path/to/somewhere", Queue[String](), ListBuffer[String](), ".svn")
print(result.size)
}
def listing(root: String, restDirs: Queue[String], files: ListBuffer[String], ignore: String): ListBuffer[String] = {
val target = new java.io.File(root)
if (target.isFile) {
files.append(root)
if (restDirs.isEmpty) {
return files
} else {
val newroot = restDirs.dequeue()
listing(newroot, restDirs, files, ignore)
}
} else {
val dirElements = target.list.toList
files.appendAll(dirElements.filter(f => new java.io.File(root + "/" + f).isFile).map(f => root + "/" + f))
val partialDirs = dirElements.filter(d => new java.io.File(root + "/" + d).isDirectory && !d.contains(ignore)).map(d => root + "/" + d)
if(restDirs.isEmpty && partialDirs.isEmpty){
return files
}else{
partialDirs.foreach(d => restDirs.enqueue(d))
val newroot = restDirs.dequeue
listing(newroot, restDirs, files, ignore)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment