Reputation: 20405
How to obtain an Array[io.BufferedSource]
to all files that match a wildcard in a given directory ?
Namely, how to define a method io.Source.fromDir
such that
val txtFiles: Array[io.BufferedSource] = io.Source.fromDir("myDir/*.txt") // ???
Noticed FileUtils in Apache Commons IO, yet much preferred is a Scala API based approach without external dependencies.
Upvotes: 10
Views: 12056
Reputation: 210812
Here is an answer based on this great answer from @som-snytt:
scala> import reflect.io._, Path._
import reflect.io._
import Path._
scala> "/temp".toDirectory.files.map(_.path).filter(name => name matches """.*\.xlsx""")
res2: Iterator[String] = non-empty iterator
as an Array:
scala> "/temp".toDirectory.files.map(_.path).filter(name => name matches """.*\.xlsx""").toArray
res3: Array[String] = Array(/temp/1.xlsx, /temp/2.xlsx, /temp/3.xlsx, /temp/a.1.xlsx, /temp/Book1.xlsx, /temp/new.xlsx)
Upvotes: 6
Reputation: 2054
Using Java 8, it is possible to traverse a directory and all it's subdirectories. Then convert the iterator to scala, and then filter according to files ending with .txt:
import scala.collection.JavaConverters._
java.nio.file.Files.walk(Paths.get("mydir")).iterator().asScala.filter(file => file.toString.endsWith(".txt")).foreach(println)
Upvotes: 3
Reputation: 39577
scala> import reflect.io._, Path._
import reflect.io._
import Path._
scala> val r = """.*\.scala""".r
r: scala.util.matching.Regex = .*\.scala
scala> "/home/amarki/tmp".toDirectory.files map (_.name) flatMap { case n @ r() => Some(n) case _ => None }
res0: Iterator[String] = non-empty iterator
scala> .toList
res1: List[String] = List(bobsrandom.scala, ...)
or recursing
scala> import PartialFunction.{ cond => when }
import PartialFunction.{cond=>when}
scala> "/home/amarki/tmp" walkFilter (p => p.isDirectory || when(p.name) {
| case r() => true })
res3: Iterator[scala.reflect.io.Path] = non-empty iterator
Upvotes: 11
Reputation: 24802
A bit rough on the edges, but maybe something like :
def getFilesMatchingRegex(dir: String, regex: util.matching.Regex) = {
new java.io.File(dir).listFiles
.filter(file => regex.findFirstIn(file.getName).isDefined)
.map (file => io.Source.fromFile(file))
}
Note that this won't fetch files in sub-directories, doesn't have more advance globbing features one might expect (à la ls ./**/*.scala
), etc…
Upvotes: 1