visitFileTree

JVM
JRE7
1.7
@ExperimentalPathApi 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.

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

Files.walkFileTree

JVM
JRE7
1.7
@ExperimentalPathApi 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
    }
}

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

Path.visitFileTree

fileVisitor