Children
Search the contents of a directory and perform operations on the objects encountered
Children
Operate on all children
import scalax.file.{Path, PathMatcher}
import scalax.file.PathMatcher._
val path:Path = Path("/tmp/")
// print the name of each object in the directory
path.children ().collect {case path => println (path.name)}
// Now print names of each directory
path.children ().collect {case IsFile(file) => println (file.name)}
Remove Spaces
Remove spaces from names of paths renaming with this method can be dangerous because the stream may be calculated lazily on some filesystems and the renamed file could also be processed resulting in a infinite loop
import scalax.file.{Path, PathMatcher}
import scalax.file.PathMatcher._
val path:Path = Path("/tmp/")
val ContainsSpace:PathMatcher = path.matcher ("* *")
path.children ().collect {case ContainsSpace (path) => path.moveTo (Path (path.name.filter (_ != ' ')))}
Count Directories
import scalax.file.{Path, PathMatcher}
import scalax.file.PathMatcher._
val path:Path = Path("/tmp/")
val fileCount: Int = path.children ().collect{case IsFile (f)=> f}.foldLeft (0){(count, _) => count+1}
Filter Contents
import scalax.file.{Path, PathMatcher}
import scalax.file.PathMatcher._
val path:Path = Path("/tmp/")
val matcher: PathMatcher = path.matcher("S*")
path.children (matcher).foreach (println _)
path.children(IsFile).foreach (println _)
Descendant Processing
All operations/filters that can be also performed on all descendants of a path. Calling the descendants method instead of children will visit all descendants of the Path rather than just the children.
import scalax.file.Path
val path:Path = Path("/tmp/")
// by default only the files contained in the current path are returned but if depth
// is set (<0 will traverse entire tree) then the stream will visit subdirectories in
// pre-order traversal
// search for a .gitignore file down to a depth of 4
val gitIgnoreRestrictedTree: Option[Path] = path.descendants (depth=4).find (_.name == ".gitignore")
// search for a .gitignore in the entire subtree
val gitIgnoreFullTree: Option[Path] = path.descendants ().find (_.name == ".gitignore")
Descendants Using Path Sets
See {creating-pathsets} for more details on creating PathSets.
This examples selects all descendants of src/main that are scala files and starts with an s.
import scalax.file.Path
val path:Path = Path("/tmp/")
path / "src" / "main" ** "s*.scala" foreach (println)