-3

I have to list out all files which are greater than particular file based on its timestamp in naming pattern in scala. Below is the example.

Files available:

log_20200601T123421.log
log_20200601T153432.log
log_20200705T093425.log
log_20200803T049383.log

Condition file:

log_20200601T123421.log - I need to list all the file names, which are greater than equal to 20200601T123421 in its name. The result would be,

Output list:

log_20200601T153432.log
log_20200705T093425.log
log_20200803T049383.log

How to achieve this in scala? I was trying with apache common, but i couldn't see greater than equal to NameFileFilter for it.

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

Perhaps the following code snippet could be a starting point:

import java.io.File

def getListOfFiles(dir: File):List[File] = dir.listFiles.filter(x => x.getName > "log_20200601T123421.log").toList

val files = getListOfFiles(new File("/tmp"))

For the extended task to collect files from different sub-directories:

import java.io.File

def recursiveListFiles(f: File): Array[File] = {
  val these = f.listFiles
  these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles) 
}

val files = recursiveListFiles(new File("/tmp")).filter(x => x.getName > "log_20200601T123421.log")
Twistleton
  • 2,735
  • 5
  • 26
  • 37
  • Thanks @Twistleton for your reply, My bad, didnt mention that, these files will be in different direcroties, like below. Directory : /tmp/log/20200601/ log_20200601T123421.log log_20200601T153432.log Directory : /tmp/log/20200705/ log_20200705T093425.log Directory : /tmp/log/20200803/ log_20200803T049383.log Could you please suggest me, is there a solution for multiple directories(subdirectories) under log directory. Thanks.. – Senthilkumaran R Aug 10 '20 at 16:27