visitFileTree

fun Path.visitFileTree(visitor: FileVisitor<Path>, maxDepth: Int = Int.MAX_VALUE, followLinks: Boolean = false)(source)

Visits this directory and all its content with the specified visitor.

The traversal is in depth-first order and starts at this directory. The specified visitor is invoked on each file encountered.

Since Kotlin

1.7

Parameters

visitor

the FileVisitor that receives callbacks.

maxDepth

the maximum depth to traverse. By default, there is no limit.

followLinks

specifies whether to follow symbolic links, false by default.

See also


fun Path.visitFileTree(maxDepth: Int = Int.MAX_VALUE, followLinks: Boolean = false, builderAction: FileVisitorBuilder.() -> Unit)(source)

Visits this directory and all its content with the FileVisitor defined in builderAction.

This function works the same as Path.visitFileTree. It is introduced to streamline the cases when a FileVisitor is created only to be immediately used for a file tree traversal. The trailing lambda builderAction is passed to fileVisitor to get the file visitor.

Example:

projectDirectory.visitFileTree {
onPreVisitDirectory { directory, _ ->
if (directory.name == "build") {
directory.toFile().deleteRecursively()
FileVisitResult.SKIP_SUBTREE
} else {
FileVisitResult.CONTINUE
}
}

onVisitFile { file, _ ->
if (file.extension == "class") {
file.deleteExisting()
}
FileVisitResult.CONTINUE
}
}

Since Kotlin

1.7

Parameters

maxDepth

the maximum depth to traverse. By default, there is no limit.

followLinks

specifies whether to follow symbolic links, false by default.

builderAction

the function that defines FileVisitor.

See also