This is the Kotlin script I’m having problems with.
import java.io.File
if( args.isEmpty()) {
println("[no args]")
} else {
println("Args:\n${args.joinToString("\n")}")
}
fun currentFolder(): File {
return File("").absoluteFile
}
val current = currentFolder()
println("Current folder: ${current.fileNames().joinToString("\n")}")
fun File.contents(): List<File> {
return this.listFiles().toList()
}
fun File.fileNames(): List<String> {
return this.files().map { it.name }
}
fun File.folderNames(): List<String> {
return this.folders().map{ it.name }
}
fun File.folders(): List<File> {
return this.contents().filter { it.isDirectory }
}
fun File.files(): List<File> {
return this.contents().filter { it.isFile }
}
fun File.printFolderInfo() {
// 1
println("Contents of '${this.name}':")
// 2
if( this.folders().isNotEmpty()) {
println("- folders:\n ${this.folderNames().joinToString("\n ")}")
}
// 3
if( this.files().isNotEmpty()) {
println("- Files:\n ${this.fileNames().joinToString("\n")}")
}
// 4
println("Parent: ${this.parentFile.name}")
}
current.printFolderInfo()
fun valueFromArgsForPrefix( prefix: String) : String? {
val arg = args.firstOrNull { it.startsWith( prefix)}
if( arg == null) return null
val pieces = arg.split("=")
return if (pieces.size == 2) {
pieces[1]
}
else {
null
}
}
val folderPrefix = "folder="
val folderValue = valueFromArgsForPrefix(folderPrefix)
if( folderValue != null) {
val folder = File(folderValue).absoluteFile
folder.printFolderInfo()
} else {
println("No path provided, printing working directory info")
currentFolder().printFolderInfo()
}
I’m meant to change the code so that if I wanted to print out the root directory which is at the top-most part of the directory tree. It does not bug out. In other words it prints out the contents.