7

I have a directory "/local/dath/mi/" which is including many files

03_ssa_fruits.csv
03_ssa_can.csv
03_ssa_veg.csv
sxattru.csv
sxprod.csv
sxstore.csv

I need to extract the list of specific files from that directory :

/local/dath/mi/03_ssa_fruits.csv
/local/dath/mi/03_ssa_can.csv
/local/dath/mi/03_ssa_veg.csv

I have tried the below code ,but it is not working.What I am doing wrong .Can anyone help me

def getListOfFiles(dir: String , naming: String): List[String] = {
      File(dir).toJava.listFiles.filter(_.isFile).toList.filter { file =>
        file.getName.startsWith("03_ss*")
      }.map(_.toString)
    }


   val listOfFile = getListOfFiles(parentDir, File(data.salFilePath).toJava.getName())
4
  • Try to remove * in file.getName.startsWith("03_ss*") Commented Jan 9, 2018 at 6:01
  • look into documentation docs.oracle.com/javase/8/docs/api/java/lang/… String object method startsWith has no support of regexp including wildcard so you need to specify just prefix 03_ss Commented Jan 9, 2018 at 6:04
  • thanks ,it is working now.My mistake.Apologies !! Commented Jan 9, 2018 at 7:25
  • os-lib is the best modern solution for performing filesystem operations like this with Scala. os-lib makes listing files so much easier and hides all the complexity of the underlying Java libs. See my answer for more details. Commented Dec 14, 2020 at 13:25

4 Answers 4

16

Here is a simple example

import java.io.File

def getListOfFiles(dir: String): List[String] = {
  val file = new File(dir)
  file.listFiles.filter(_.isFile)
    .filter(_.getName.startsWith("03_ss"))
    .map(_.getPath).toList
}
getListOfFiles("directory path")

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

2

os-lib is the easiest, most elegant way to perform Scala filesystem operations.

Here's how to list the files that start with "03_ss":

os.list(os.pwd/"local_data").filter(_.segments.toList.last.startsWith("03_ssa"))

This solution is powerful because it lets you perform additional filesystem operations on the files that start with "03_ssa". You can move these files to an other directory, get the file sizes, rename them, delete them, whatever you want.

It's not like the other solutions that simply returns a list of strings. See here for more info on how to harness the power of os-lib.

Here's the code snippet to setup a folder with OP's files if you'd like to experiment on your own machine:

os.makeDir(os.pwd/"local_data")
os.write(os.pwd/"local_data"/"03_ssa_fruits.csv", "")
os.write(os.pwd/"local_data"/"03_ssa_can.csv", "")
os.write(os.pwd/"local_data"/"03_ssa_veg.csv", "")
os.write(os.pwd/"local_data"/"sxattru.csv", "")
os.write(os.pwd/"local_data"/"sxprod.csv", "")
os.write(os.pwd/"local_data"/"sxstore.csv", "")

Comments

0
def pathStringToPath(pathString: String): Path = Paths.get(pathString)

def pathStringToDir(pathString: String): Option[File] = pathToDir(pathStringToPath(pathString))

def pathToDir(path: Path): Option[File] = {
  val file = path.toFile
  file.isDirectory match {
    case true => Some(file)
    case false => None
  }
}

def filesInDir(pathString: String): List[File] = {
  val dirOption = pathStringToDir(pathString)
  dirOption.fold(List.empty[File])(dir => dir.listFiles().toList)
}

def filesWithPrefixInDir(dirPathString: String, fileNamePrefix: String): List[File] =
  filesInDir(dirPathString).filter(file => file.getName.startsWith(fileNamePrefix))

filesWithPrefixInDir(parentDir, "03_ss")

Comments

0
def fetchFilesFromLocalDirectory(path : String) : List[File] = {
        val path = Paths.get(path)

        val file = path.toFile
        if(file.isDirectory){
            file.listFiles().filter(_.getName.endsWith(".txt")).toList
        }else List[File]() // TODO: If is not a directory, it should throw an error
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.