Search code examples
scalapathescapingfilepathstring-literals

Scala - write Windows file paths that contain spaces as string literals


I need to make some Windows file paths that contain spaces into string literals in Scala. I have tried wrapping the entire path in double quotes AND wrapping the entire path in double quotes with each directory name that has a space with single quotes. Now it is wanting an escape character for "\Jun" in both places and I don't know why.

Here are the strings:

    val input = "R:\'Unclaimed Property'\'CT State'\2015\Jun\ct_finderlist_2015.pdf"
    val output = "R:\'Unclaimed Property'\'CT State'\2015\Jun"

Here is the latest error: enter image description here


Solution

  • The problem is with the \ character, that has to be escaped. This should work:

    val input = "R:\\Unclaimed Property\\CT State\\2015\\Jun.ct_finderlist_2015.pdf"
    val output = "R:\\Unclaimed Property\\CT State\\2015\\Jun"