I think this method can be simplified a bit, maybe something along these lines...

```
  def ensureFolderExists(container: IContainer, segments: List[String]): IFolder = {
     if (!container.exists) container.create(..)
    
     segments match {
       case Nil =>
       case segment :: rest =>
         ensureFolderExists(container.getFolder(segment), segments.tail)
     }
    }
    ensureFolderExists(top, path.segments.toList.drop(1))
```