I have a directory that contains a list of files. I wanted to get the latest file out of all the contents of the said directory. How will I do that?
I am using this code, but I am not getting the latest file out of it. Please help.
def fileDir = new File("A/B").listFiles().first()
Thanks.
As simple as:
new File( 'A/B' ).listFiles()?.sort { -it.lastModified() }?.head()
(taking the negative lastModified, as we want the newest file first)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With