January 2017
Intermediate to advanced
420 pages
9h 25m
English
Kotlin can also be run as a script. If bash or Perl is not for you, now you have an alternative.
Say you want to delete all the files older than N given days. The following code example does just that:
import java.io.File
val purgeTime = System.currentTimeMillis() - args[1].toLong() * 24 * 60 * 60 * 1000
val folders = File(args[0]).listFiles { file -> file.isFile }
folders ?.filter {
file -> file.lastModified() < purgeTime }
?.forEach {
file -> println("Deleting ${file.absolutePath}")
file.delete()
}
Create a file named delete.kts with the preceding content. Please note the predefined variable args, which contains all the incoming parameters passed when it is invoked. You might wonder what is the ? character doing there. If ...
Read now
Unlock full access