copyToRecursively

fun Path.copyToRecursively(target: Path, onError: (source: Path, target: Path, Exception) -> OnErrorResult = { _, _, exception -> throw exception }, followLinks: Boolean, overwrite: Boolean): Path(source)

Recursively copies this directory and its content to the specified destination target path. Note that if this function throws, partial copying may have taken place.

Unlike File.copyRecursively, if some directories on the way to the target are missing, then they won't be created automatically. You can use the following approach to ensure that required intermediate directories are created:

sourcePath.copyToRecursively(
destinationPath.apply { parent?.createDirectories() },
followLinks = false
)

If the entry located by this path is a directory, this function recursively copies the directory itself and its content. Otherwise, this function copies only the entry.

If an exception occurs attempting to read, open or copy any entry under the source subtree, further actions will depend on the result of the onError invoked with the source and destination paths, that caused the error, and the exception itself as arguments. If onError throws, this function ends immediately with the exception. By default onError rethrows the exception. See OnErrorResult for available options.

This function performs "directory merge" operation. If an entry in the source subtree is a directory and the corresponding entry in the target subtree already exists and is also a directory, it does nothing. Otherwise, overwrite determines whether to overwrite existing destination entries. Attributes of a source entry, such as creation/modification date, are not copied. followLinks determines whether to copy a symbolic link itself or its target. Symbolic links in the target subtree are not followed.

To provide a custom logic for copying use the overload that takes a copyAction lambda.

Since Kotlin

1.8

Parameters

target

the destination path to copy recursively this entry to.

onError

the function that determines further actions if an error occurs. By default, rethrows the exception.

followLinks

false to copy a symbolic link itself, not its target. true to recursively copy the target of a symbolic link.

overwrite

false to throw if a destination entry already exists. true to overwrite existing destination entries.

Throws

if the entry located by this path does not exist.

if target is an entry inside the source subtree.

if a destination entry already exists and overwrite is false. This exception is passed to onError for handling.

if any errors occur while copying. This exception is passed to onError for handling.

if a security manager is installed and access is not permitted to an entry in the source or target subtree. This exception is passed to onError for handling.


fun Path.copyToRecursively(target: Path, onError: (source: Path, target: Path, Exception) -> OnErrorResult = { _, _, exception -> throw exception }, followLinks: Boolean, copyAction: CopyActionContext.(source: Path, target: Path) -> CopyActionResult = { src, dst -> src.copyToIgnoringExistingDirectory(dst, followLinks) }): Path(source)

Recursively copies this directory and its content to the specified destination target path. Note that if this function throws, partial copying may have taken place.

Unlike File.copyRecursively, if some directories on the way to the target are missing, then they won't be created automatically. You can use the following approach to ensure that required intermediate directories are created:

sourcePath.copyToRecursively(
destinationPath.apply { parent?.createDirectories() },
followLinks = false
)

If the entry located by this path is a directory, this function recursively copies the directory itself and its content. Otherwise, this function copies only the entry.

If an exception occurs attempting to read, open or copy any entry under the source subtree, further actions will depend on the result of the onError invoked with the source and destination paths, that caused the error, and the exception itself as arguments. If onError throws, this function ends immediately with the exception. By default onError rethrows the exception. See OnErrorResult for available options.

Copy operation is performed using copyAction. By default copyAction performs "directory merge" operation. If an entry in the source subtree is a directory and the corresponding entry in the target subtree already exists and is also a directory, it does nothing. Otherwise, the entry is copied using sourcePath.copyTo(destinationPath, *followLinksOption), which doesn't copy attributes of the source entry and throws if the destination entry already exists. followLinks determines whether to copy a symbolic link itself or its target. Symbolic links in the target subtree are not followed.

If a custom implementation of copyAction is provided, consider making it consistent with followLinks value. See CopyActionResult for available options.

If copyAction throws an exception, it is passed to onError for handling.

Since Kotlin

1.8

Parameters

target

the destination path to copy recursively this entry to.

onError

the function that determines further actions if an error occurs. By default, rethrows the exception.

followLinks

false to copy a symbolic link itself, not its target. true to recursively copy the target of a symbolic link.

copyAction

the function to call for copying source entries to their destination path rooted in target. By default, performs "directory merge" operation.

Throws

if the entry located by this path does not exist.

if target is an entry inside the source subtree.

if any errors occur while copying. This exception is passed to onError for handling.

if a security manager is installed and access is not permitted to an entry in the source or target subtree. This exception is passed to onError for handling.