[ad_1]
os-lib is the easiest way to recursively list files in Scala.
os.walk(os.pwd/"countries").filter(os.isFile(_))
Here’s how to recursively list all the files that match the "a*.foo"
pattern specified in the question:
os.walk(os.pwd/"countries").filter(_.segments.toList.last matches "a.*\\.foo")
os-lib is way more elegant and powerful than other alternatives. It returns os
objects that you can easily move, rename, whatever. You don’t need to suffer with the clunky Java libraries anymore.
Here’s a code snippet you can run if you’d like to experiment with this library on your local machine:
os.makeDir(os.pwd/"countries")
os.makeDir(os.pwd/"countries"/"colombia")
os.write(os.pwd/"countries"/"colombia"/"medellin.txt", "q mas pues")
os.write(os.pwd/"countries"/"colombia"/"a_something.foo", "soy un rolo")
os.makeDir(os.pwd/"countries"/"brasil")
os.write(os.pwd/"countries"/"brasil"/"a_whatever.foo", "carnaval")
os.write(os.pwd/"countries"/"brasil"/"a_city.txt", "carnaval")
println(os.walk(os.pwd/"countries").filter(os.isFile(_)))
will return this:
ArraySeq(
/.../countries/brasil/a_whatever.foo,
/.../countries/brasil/a_city.txt,
/.../countries/colombia/a_something.foo,
/.../countries/colombia/medellin.txt)
os.walk(os.pwd/"countries").filter(_.segments.toList.last matches "a.*\\.foo")
will return this:
ArraySeq(
/.../countries/brasil/a_whatever.foo,
/.../countries/colombia/a_something.foo)
See here for more details on how to use the os-lib.
[ad_2]