Search code examples
scalaio

Search a folder with specific name in Scala


There are many folders contains the daily build artifacts. I need to get the newest folder to test some parts inside. The name of the fold has the following style

xx.172-3 

xx is a stable number and can be ignored. 172 is the 172th build chronologically. 3 is the 3rd version of 172. The length of the folder name is not consistent. There might have 34.99-0 before 34.120-11

A typical folders, under for example /test path, are

   /test |-/34.170-0/ 
         |-/34.171-0/  
         |-/34.171-1/ 
         |-/34.172-0/
         |-/34.171-1/ 
         |-/34.171-2/
         |-/34.171-3/

Question:

How can I find the latest build folder /34.171-3 by using Scala?

Note:

I've thought about using the time stamp and it doesn't work. Because previous folder is free to access after build. The time stamp does not tell if it is the newest build


Solution

  • You might use a Regex pattern to make sure the directory name is safe for comparison.

    val dirPattern = raw"\d+.(\d+)-(\d+)".r
    
    listOfDirNames.maxBy{
      dirPattern.unapplySeq(_)
                .collect{case List(a,b) => (a.toInt,b.toInt)}
                .getOrElse((0,0))
    }