0

My problem statement is as below:

1) I have a path till a folder. I have to traverse using that path. Check whether there are any subfolders and files in that path. If yes, match the contents of the folder(Lists) with the array. If it matches take it as a new path.

2) Using new path. List down the files in that path.

I am able to do everything except comparing lists and array. Below is my code:

import java.text.SimpleDateFormat
import java.util.Calendar
import java.io.File

class ListingDirectories {
  def getListOfDirectories(dir: String): List[File] = {
    val d = new File(dir)
    if (d.exists && d.isDirectory) {
      d.listFiles().filter(_.isDirectory()).toList
    } else {
      List[File]()
    }
  }
  def getListOfFiles(dir: String): List[File] = {
    val d = new File(dir)
    if (d.exists && d.isDirectory()) {
      d.listFiles().filter(_.isFile()).toList
    } else {
      List[File]()
    }
  }
}

object FirstSample {
  def main(args: Array[String]) {
    val ld = new ListingDirectories()
    val directoriesList = ld.getListOfDirectories("C:/Users/Siddheshk2/Desktop/CENSUS").toList
    println(directoriesList + "\n")
    val directoriesListReplaced = directoriesList.toString().replace("//", "/")
    // println(directoriesListReplaced.indexOf("C:/Users/Siddheshk2/Desktop/CENSUS/SAMPLE"))

    var finalString = ""
    var s = Array("C:/Users/Siddheshk2/Desktop/CENSUS/SAMPLE")
    for (x <- s) {
      if (x.equals(directoriesListReplaced)) {
        finalString = s(0)

      } else {
        println("No matching strings")
      }
    }
    val filesList = ld.getListOfFiles(finalString)
    println(filesList.toString())

  }
}  

I just need to compare the values from the list and array and take it as a new path in finalString variable in order to pass in the next method which is getListOfFiles. I figured out since I am returning List[file] in methods I am not able to access the elements inside it. Can anyone help me to understand where am I going wrong? TIA

4
  • 1
    could you share the Error stack-trace that you have encountered? Commented Sep 27, 2019 at 8:41
  • There is no error. Code works fine but goes in to the else loop. Nothing else. Commented Sep 27, 2019 at 8:45
  • The code works as is for me too. does the folder SAMPLE exists inside CENSUS? Commented Sep 27, 2019 at 9:00
  • Yes, there are 3 folders inside CENSUS named SAMPLE,SAMPLE1,SAMPLE2. I have to compare values which is in array (its the value which I want to compare ) and file list values Commented Sep 27, 2019 at 9:14

2 Answers 2

1

Your directoriesListReplaced will be a string which looks like "List(C:/Users/Siddheshk2/Desktop/CENSUS/SAMPLE,C:/Users/Siddheshk2/Desktop/CENSUS/SAMPLE1,C:/Users/Siddheshk2/Desktop/CENSUS/SAMPLE2)" and s won't equal it. It isn't at all clear what you want to do with directoriesListReplaced; maybe it should just be

for (x <- s) {
  if (directoriesList.contains(x)) {
    ...
  } else {
    println("No matching strings")
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I edited your code according to mine and it gives me only 'C' value when I try to print finalString var. So, it gives value at first index
That's what s(0) does, yes (or x(0) in the case of your edit): first character of the string. You just want finalString = x (and change the condition).
It was not working because of the return type in the methods. So, I simplified it and got the results
0

After lot of struggle, I solved it using below code:

import java.io.File
object PlayingWithLists {
  def main(ar: Array[String]) {
    var s = "C:/Users/Siddheshk2/Desktop/CENSUS/SAMPLE2"
    var finalValue = ""
    var valuesReplaced = ""
    var filePath = ""
    for (file <- new File("C:/Users/Siddheshk2/Desktop/CENSUS").listFiles) {
      valuesReplaced = file.toString.replace("\\", "/")

      if (valuesReplaced.contains(s.trim)) {
        finalValue = file.toString
      } else {

      }
    }
    for (file <- new File(finalValue).listFiles) {
      filePath = file.toString.trim
    }
  }
}

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.