0

I am trying to read a file which has name: K2ssal.timestamp. I want to handle the time stamp part of the file name as wildcard. How can I achieve this ? tried * after file name but not working.

var getK2SSal: Iterator[String] = Source.fromFile("C://Users/nrakhad/Desktop/Work/Data stage migration/Input files/K2Ssal.*").getLines()
RobJan
  • 1,351
  • 1
  • 14
  • 18
Nik
  • 1
  • hi and welcome: please read here -> https://stackoverflow.com/help/how-to-ask , then edit your question – Leviand Jul 20 '18 at 11:10
  • duplicate of https://stackoverflow.com/questions/2637643/how-do-i-list-all-files-in-a-subdirectory-in-scala ? – joel Jul 20 '18 at 11:11
  • perhaps search for files in that directory with such names then open whatever files match – joel Jul 20 '18 at 11:43

3 Answers3

2

You can use Files.newDirectoryStream with directory + glob:

import java.nio.file.{Paths, Files}

val yourFile = Files.newDirectoryStream(
  Paths.get("/path/to/the/directory"),       // where is the file?
  "K2Ssal.*"                                 // glob of the file name
).iterator.next                              // get first match
Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93
1

Misconception on your end: unless the library call is specifically implemented to do so, using a wildcard simply doesn't work like you expect it to.

Meaning: a file system doesn't know about wildcards. It only knows about existing files and folders. The fact that you can put * on certain commands, and that the wildcard is replaced with file names is a property of the tool(s) you are using. And most often, programming APIs that allow you to query the file system do not include that special wild card handling.

In other words: there is no sense in adding that asterisk like that.

You have to step back and write code that actively searches for files itself. Here are some examples for scala.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

You can read the directory and filter on files based upon the string.

val l = new File("""C://Users/nrakhad/Desktop/Work/Data stage migration/Input files/""").listFiles
val s = l.filter(_.toString.contains("K2Ssal."))
KarateKid
  • 3,138
  • 4
  • 20
  • 39