0

I have stuck with a requirement. I need to list all the files that ends with .txt and some time .srt etc.

I have to show the list of files after scanning through all the directories that are present.

I know how to scan files in a directory.

val dir = File(dirName)

        return dir.listFiles(object : FilenameFilter() {
            override fun accept(dir: File?, filename: String): Boolean {
                return filename.endsWith(".txt")
            }
        })

But I want to scan all the directories for files that ends with specified extension.

And also I need to edit it. So, Some how I need to get their path.

How can I achieve this in kotlin.

  • https://stackoverflow.com/questions/44567419/list-files-recursively-in-kotlin this may help – HED Aug 27 '20 at 15:28
  • @HED I typed `File("/tmp").walkTopDown().forEach { Log.d("log", "$it") }` in on create of the view. and it prints nothing. I need more explanation for this. Thank you. –  Aug 27 '20 at 15:37
  • Im not aware that there would be a /tmp directory on an Android device. Try with another directory. – blackapps Aug 27 '20 at 15:48
  • @blackapps I think you didn't understood the problem. I need to search all the directories that are present. ex: movies - downloads etc. If i specify a directory that will search only on that particular directory like "downloads". I need a way to mention root folder name which contains all these directories. and loop through –  Aug 28 '20 at 01:39
  • @blackapps may be this will be the answer but i'm not getting it properly. https://stackoverflow.com/questions/39484758/how-to-get-paths-of-folders-that-has-image-or-video/39485395 –  Aug 28 '20 at 01:50
  • `If i specify a directory that will search only on that particular directory like "downloads".` Even if the downloads directory has subdirectories ans so on? Then you have to make your function recursive. – blackapps Aug 28 '20 at 07:32
  • @blackapps This is becoming very tough. I will change my requirement itself. Thank you ! –  Aug 28 '20 at 07:46

0 Answers0