Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the latest file from a directory using groovy?

Tags:

file

groovy

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.

like image 811
chemilleX3 Avatar asked Dec 12 '22 20:12

chemilleX3


1 Answers

As simple as:

new File( 'A/B' ).listFiles()?.sort { -it.lastModified() }?.head()

(taking the negative lastModified, as we want the newest file first)

like image 148
tim_yates Avatar answered Dec 22 '22 01:12

tim_yates