0

i want to list files of a Windows OS folder. I want to use Windows file addresses in Kotlin without using a double backslash. I do not want Kotlin to interpret the backslash here.

My best result for the moment you find at the end of my question.

Error: (3, 24) Kotlin: Illegal escape: '\ M' returns the following:

fun main(args: Array<String>) {
    var s: String = "G:\My Web Sites\"
    println(s)
}

I do not want to unmask the backslash with a backslash every time. I think it looks not good and can not easily copy back and forth.

works but is not pretty:

fun main(args: Array<String>) {
    var s: String = "G:\\My Web Sites\\"
    println(s)
}

walkaround:

Maybe I'll do it until I find another solution.

May there is a chance to switch the File.separator ?

of course this gaves me the same error, Because the replacement is too late:

import java.io.File
fun main(args: Array<String>) {
    var s: String = "G:\My Web Sites\"
    val replace = s.replace('/', File.separatorChar);
    println(replace)
}

best result:

the best result i got is the following. it returns the correct file sting, but returns now files inside the folder. it returns the correct string: "G:\\My Web Sites\\"

import java.io.File

fun main(args: Array<String>) {
    var s = """"G:\My Web Sites\"""
    println(s)
    s= s.replace("\\", "\\\\")
    println(s)
    File(s).walk().forEach  { println(it) }
}


private fun String.replace(regex: Regex, notMatched: (String) -> String, matched: (MatchResult) -> String): String {
//    its from https://github.com/http4k/http4k/blob/master/http4k-core/src/main/kotlin/org/http4k/core/UriTemplate.kt
    val matches = regex.findAll(this)
    val builder = StringBuilder()
    var position = 0
    for (matchResult in matches) {
        val before = substring(position, matchResult.range.start)
        if (before.isNotEmpty()) builder.append(notMatched(before))
        builder.append(matched(matchResult))
        position = matchResult.range.endInclusive + 1
    }
    val after = substring(position, length)
    if (after.isNotEmpty()) builder.append(notMatched(after))
    return builder.toString()
}
SL5net
  • 2,282
  • 4
  • 28
  • 44

1 Answers1

1

You should use slash in your code, the JVM will automatically convert it to backslash on Windows (see Forward slash or backslash?).

If you really like to use backslash, raw strings """ do the work and you don't need to replace it manually afterwards.

Rene
  • 5,730
  • 17
  • 20
  • No, not here. Here only the variant with prepared string works (is shown below and commented out): https://github.com/sl5net/UnitTest4_g-IntelliSense-everywhere/blob/master/src/parse_AHK_helpfile.kt – SL5net Oct 16 '18 at 08:38
  • works https://github.com/sl5net/UnitTest4_g-IntelliSense-everywhere/commit/499caca7f1647b6f039bb14ae023df8a1021196f – SL5net Oct 16 '18 at 09:47
  • is correct `"""G:\My Web Sites\""" is wrong """"G:\My Web Sites\"""` – SL5net Oct 16 '18 at 09:51
  • \\ also seems to be correct http://kotlinlang.org/docs/reference/basic-types.html#characters – Martin Zeitler Oct 16 '18 at 11:45