Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.89 KB | None | 0 0
  1. import scala.reflect.io._
  2.  
  3. object App extends App {
  4.     val desktopStructure = new FolderStructure("C:/Users/MyUser/Desktop")
  5. }
  6.  
  7. case class TreeNode[T](id: String, parent_id: String, value: T)
  8.  
  9. class FolderStructure(rootDirectory: String) {
  10.     val separator = '\\'   
  11.     val folders = Directory(rootDirectory).deepList().filter(_.isDirectory).map(pathToTreeNode).toList
  12.     val files = Directory(rootDirectory).deepFiles.toList.map(pathToTreeNode)
  13.  
  14.     def pathToTreeNode = (path: Path) => {
  15.         val relativePath = path.toString.substring(rootDirectory.length + 1)
  16.         relativePath.indexOf(separator) match {
  17.             case -1 =>
  18.                 TreeNode(relativePath, "", relativePath)
  19.             case _ =>
  20.                 val split = relativePath.split(separator)
  21.                 val lastDir = split(split.length - 1)
  22.                 val parent = relativePath.substring(0, relativePath.length - lastDir.length - 1)
  23.                 TreeNode(relativePath, parent, lastDir)
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement