So I have a bunch of archives in a directory that look like this:
test_61995.zip test_61234.zip test_61233.zip
I want to copy only the latest file from here using Gradle. Is is possilbe to sort the files and date and time and copy usng gradle?
Sure, you can do that. Here is an example
Kotlin DSL:
tasks {
val cp by creating(Copy::class.java) {
from(File("/home/madhead/Downloads/").listFiles().sortedBy { it.lastModified() }.last())
into(File("/home/madhead/Downloads/so53777253/"))
}
}
Groovy DSL:
task cp(type: Copy) {
from(new File("/home/madhead/Downloads/").listFiles().sort{ it.lastModified() }[0])
into(new File("/home/madhead/Downloads/so53777253/"))
}
This will copy the latest modified file from /home/madhead/Downloads/
to /home/madhead/Downloads/so53777253/
.