WALKTHROUGH A FOLDER.

MAKU
2 min readFeb 3, 2020

The second easiest program to write, after “hello world” in KOTLIN.

I've been tasked to convert images from png and jpg to webp in an android app. I've converted most of them, but before I move that Trello item “convert all images to webp” from IN PROGRESS to DONE, I have to make sure all images have been converted.

So, below is a short program to loop through all the files in the res folder, and yes… I got the culprits.

import java.io.Filefun main(args: Array<String>) {println("Hello, World, lets filter the odd images out..")try {// using extension function walkFile("/home/maku/Documents/<your_folder_location>").walk().forEach {if(it.extension == "png" || it.extension == "jpg")println(it.extension + " is the extension of " + it.name)}} catch (e : Exception){println("Exception is handled." + e)}}

Creating the file filter program using the command-line compiler.

First, we download the latest versionkotlin-compiler-<x.x.x>.zip from here DOWNLOAD, and make sure to run the simple hello world program in your favorite editor (me -> VSCODE).

Next, search Google or Stackoverflow on how to traverse through a folder in Kotlin. Read the Official docs to get an understanding of the concept, and this simplified tutorial to how to implement the walk() — using java.io.File.walk().

This approach beats the manual way, any day.
Thank you for reading.

--

--