Ged
Ged

Reputation: 18098

SCALA contains multiple conditions on listFiles

The following works, but I would like to add a list of conditions for the contain. That I cannot seem to effect.

val Files = dir
  .listFiles
  .filter(_.getName.contains("INIT"))
  .sorted
  .map(f => f.toString)

I would like to check for INIT and UPD and DEL in the most efficient way. All options I have tried have not yielded a result.

Upvotes: 1

Views: 289

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44957

You want to check whether there exists a substring from a list of given substrings such that the filename contains the substring. In code:

val substrings = List("INIT", "UPD", "DEL")
dir
  .listFiles
  .filter(f => { 
    val n = f.getName
    substrings.exists(n.contains)
  })
  .sorted
  .map(f => f.toString)

Upvotes: 1

Related Questions