如何使用groovy从目录中获取最新的文件? [英] How to get the latest file from a directory using groovy?
本文介绍了如何使用groovy从目录中获取最新的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含文件列表的目录。我想从所述目录的所有内容中获取最新的文件。我将如何做到这一点?
我正在使用这个代码,但我没有得到最新的文件。 (b)
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ def $ file $ b
谢谢。
解决方案
新建文件('A / B').listFiles()?。sort {-it .lastModified()} ?. head()
(以lastModified为负数,因为我们想要最新的文件第一)
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)
这篇关于如何使用groovy从目录中获取最新的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文