Table of Contents
Here’s the way to copy directory recursively including dot files in Kotlin. I wanted do that with command but I couldn’t. So I used Kotlin.
Environment
- Kotlin 1.1.2
Code to Copy Directory
I used java.io.File
, so it is intrinsically the same as in Java.
1 2 3 4 5 |
import java.io.File val s = File("source_directory") val d = File("destination_directory") s.copyRecursively(d, true) |
Supplement
Second argument of copyRecursively
method defines to not to override when the file exists at the copy destination.
Copy All Files and Directories in the Directory
1 2 3 4 5 |
import java.io.File val s = File("source_directory") val d = File("destination_directory") s.listFiles().forEach { it.copyRecursively(File(File(d.absolutePath).parent + '/' + it.name)) } |
The first argiment copyRecursively
receives is complicated, but it’s for the case when d
is defined with relative path.