Library contents

Kotlin Path.createDirectories

Create a nested directory tree and verify the final directory.

A simple example

Path.createDirectories

  1. Start with An empty practice folder
  2. Do this Create a/b including parents
  3. Result Both directories exist
Creating parents allows b to be placed inside a even when a was initially missing.

Syntax

Kotlin syntax
week.createDirectories()
Run these examples in Kotlin

Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.

kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jar

Prepare nested hand-in folders

A department creates a hand-in folder for each class and week. The job runs again if the first delivery is interrupted, so existing directories should remain usable on a second run.

Resultready
Lesson.kt
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.isDirectory

fun main() {
    val base = Files.createTempDirectory("lesson-")
    try {
        val week = base.resolve("class/week1")
        repeat(2) { week.createDirectories() }
        if (week.isDirectory()) println("ready")
    } finally {
        base.toFile().deleteRecursively()
    }
}
Output
ready

The second attempt finds the same directory and the application can use it.

Advertisements

Locate a rehearsal score

The choir stores each score inside a season folder. Create the named season or class folder and its proofs subfolder in an empty workspace. What state should the operator see after successful creation?

Givenspring / alto.txt
Worked resultFolder ready

Locate a workshop handout

An instructor stores the current template in a class folder. Create the named season or class folder and its proofs subfolder in an empty workspace. What state should the operator see after successful creation?

Givenceramics / glaze.pdf
Worked resultFolder ready

What to watch for

Handle a regular file occupying any required directory name as an error.

Documentation for Path.createDirectories