Q:

Transform directory to object in Kotlin

I'm trying to parse some files this way:
File(tentConfig.getPathRepository())
            .walkTopDown()
            .forEach { file -> processFile(file) }

the path of this file is: /communications/email/begginer/.file
I have to convert that path to object like this:
my communications should be my category, email should be a subcategory of communications and beginner subcategory of email. 
my process method is responsible to serialize this path to object but I'm pretty sure there is a better solution. 
private fun processCategory(currentFile: File) {
    val listOfDirectory = currentFile.path.split("/".toRegex())

    listOfDirectory.forEachIndexed { index, folderName ->
        if (index == 0) {
            val currentCategory = parseYmlFile(currentFile, Category::class)
            lesson.categories.forEach { itCategory ->
                if (itCategory.title ! = currentCategory.title) lesson.categories.add(currentCategory)
            }

        } else {
            val subCategory = parseYmlFile(currentFile, Category::class)
            lesson.categories[subCategory.index].subcategories.add(subCategory)
        }
    }
}

A:

For the sake of demo/testing purposes, my implementation of Category might be different from yours.  Here's the one I was using:
inner class Category(val s: String, var subCategory: Category?  = null)

Now that being said, here's a little function that will loop through the path of the given File, and construct a Category hierarchy, placing each element in the right order:
private fun processCategory(currentFile: File): Category? { 
        val listOfDirectory = currentFile.path.split("/".toRegex())

        //The root category (in your example, communications)
        var rootCategory: Category?  = null
        //A reminder of the current Category, so we can attach the next one to it
        var currentCategory: Category?  = null
        listOfDirectory.forEach {
            if (rootCategory == null) {
                //First element, so I need to create the root category
                rootCategory = Category(it)
                currentCategory = rootCategory
            } else {
                //Other elements are simply created
                val nextCategory = Category(it)
                //Added as a subCategory of the previous category
                currentCategory!!.subCategory = nextCategory
                //And we progress within the chain
                currentCategory = nextCategory
            }
        }
        //In the end, my root category will contain :
        // Category("communications", Category("email", Category("Beginner", null)))
        return rootCategory
    }

You can surely adapt this code to your needs, by replacing the constructor that I'm using with your YmlParser

