Source Directory is deleted whenever File is moved from Target Directory [Kotlin]

90 Views Asked by At

When ever I run the program the files will successfully move to the intended target directory.

However while doing so the source directory is deleted, The purpose of the code is to move txt files to the intended directory

import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.file.StandardCopyOption


fun main(args: Array<String>) {

    val sourcePath : String = "/home/linuxwithish/TestStart"
    val targetPath : String = "/home/linuxwithish/Test"

    val fSP = Paths.get(sourcePath)
    val fTP = Paths.get(targetPath)

    File(sourcePath).walk().forEach {
        when (it.extension) {
            "txt" -> Files.move(fSP,fTP,StandardCopyOption.Replacing_EXISTING)

        }
    }
}
1

There are 1 best solutions below

0
Icestope419 On

In hindsight, I was only moving the folder instead of actually calling the file.