Files and Directories Listing

I have the following script:

import java.io.File
if( args.isEmpty()) {
    println("[no args]")
} else {
    println("Args:\n${args.joinToString("\n")}")
}
fun charInt( n: Int ) : Int {
    var intChar = n
    if( intChar > 'z'.toInt()) {
        intChar = 'a'.toInt() + n % ('z'.toInt() - 'a'.toInt())
    }
    return intChar
}
fun encryptString( string: String, n: Int) : String {
    var encryptedString: List<Char>
    encryptedString = string.map( { (charInt(it.toInt() + n)).toChar() } )
    return encryptedString.toString()
}


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( progparam: String?) {
    //val arg = args.SecondOrNull { it.startsWith( this.path)}
    // 1
    var displayHidden = false
    if( progparam != null) {
        if( progparam.toString() == "true") {
            displayHidden = true
        }
    }
    println("Contents of '${this.name}':")
    // 2
    if (this.folders().isNotEmpty()) {
        for( i in 0..this.folders().size - 1) {
            if (this.folders()[i].name[0] != '.' || displayHidden) {
                println("- folders:\n    ${this.folderNames().joinToString("\n    ")}")
            }
        }
    }

    // 3
    if( this.files().isNotEmpty()) {
        for (i in 0..this.files().size - 1) {
            if (this.files()[i].name[0] != '.' || displayHidden ) {
                println("- Files:\n    ${this.fileNames().joinToString("\n")}")
            }
        }
    }
    // 4
    println("Parent: ${this.parentFile.name}")
}
current.printFolderInfo(null)

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 folderPrefix2 = "hidden="
val folderPrefix3 = "rotation="
val folderValue = valueFromArgsForPrefix(folderPrefix)
val folderValue2 = valueFromArgsForPrefix(folderPrefix2)
val folderValue3 = valueFromArgsForPrefix(folderPrefix3)
var printedDirectoriesEtc = false
if( folderValue != null) {
    val folder = File(folderValue).absoluteFile
    folder.printFolderInfo(null)
    printedDirectoriesEtc = true
} else {
    println("No path provided, printing working directory info")
    currentFolder().printFolderInfo(null)
    printedDirectoriesEtc = true
}
if( folderValue2 != null && printedDirectoriesEtc == false) {
    val folder2 = File(folderValue2).absoluteFile
    folder2.printFolderInfo(folderValue2)
}
val string: String = "hereisastring"
var encodedString: String = ""
if( folderPrefix3 != null) {
    encodedString = encryptString(string, folderValue3!!.toInt())
}
println("$encodedString")

I’m trying to write a script that uses the ROT-n encoding scheme with the one difference being that if you try to rotate a letter by n and that value ends up in being a value outside the 26 char alphabet it rotates around.

The program basically works It encoded a string and it is not far off a correct value.

Any help would be very welcome.

1 Like

There is a lot of code there, and I’m pretty sure most of it is not related to the actual problem.
Could you please post an expected input, expected output, and what are you getting?

1 Like