relativeToOrNull

Calculates the relative path for this path from a base path.

fun Path.relativeToOrNull(base: Path): Path?(source)
import kotlin.io.path.Path
import kotlin.io.path.relativeToOrNull

fun main() {
    val base = Path("/home/user")
    val file = Path("/home/user/docs/report.pdf")

    val relative = file.relativeToOrNull(base)
    println(relative)          // prints: docs/report.pdf

    val unrelated = Path("/var/log")
    println(unrelated.relativeToOrNull(base)) // prints: null
}

Source