How to get latest modified file using scala from a folder in HDFS

31 views Asked by At

being pretty new to Scala can any one please kindly help me with a scenario where I need to get the file with latest modified date.

There are two scenarios. consider 2 directories ,

  • dir1 (which has sub directories and files present inside each sub directory )
  • dir2 (no sub directories but has only files )

for Scenario 1 - I need to get the file with latest modified date inside a latest modified date directory

for Scenario 2 - I just need the latest file with latest modified.

Please do help me in any way possible, I tried experiments with java.io file handling but haven't made any good progress.

adding the code I tried

1

There are 1 answers

3
Tim On

This is my (untested) code to perform this operation on nested directories:

def latest(file: File): File =
  file.listFiles.maxByOption(_.lastModified) match {
    case Some(f) => latest(f)
    case None => file
  }

This is tail recursive and will compile to a simple loop.