Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import scala.reflect.io._
- object App extends App {
- val desktopStructure = new FolderStructure("C:/Users/MyUser/Desktop")
- }
- case class TreeNode[T](id: String, parent_id: String, value: T)
- class FolderStructure(rootDirectory: String) {
- val separator = '\\'
- val folders = Directory(rootDirectory).deepList().filter(_.isDirectory).map(pathToTreeNode).toList
- val files = Directory(rootDirectory).deepFiles.toList.map(pathToTreeNode)
- def pathToTreeNode = (path: Path) => {
- val relativePath = path.toString.substring(rootDirectory.length + 1)
- relativePath.indexOf(separator) match {
- case -1 =>
- TreeNode(relativePath, "", relativePath)
- case _ =>
- val split = relativePath.split(separator)
- val lastDir = split(split.length - 1)
- val parent = relativePath.substring(0, relativePath.length - lastDir.length - 1)
- TreeNode(relativePath, parent, lastDir)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement