#2083 Problem with music sharp sign (#, U+266F) in file names on Windows

ortizgiraldo Wed 16 Jan 2013

Hi,

I need to process a bunch of files that have a # character in their name. Those files are on a Windows XP box. The little script that I wrote in Fantom is not finding them because is changing the path by either dropping everything after the # character or by escaping it and changing the separators. This sample class shows what is happening:

class MyClass
{
  static Void main() {
    uri := `/tmp/foo/bar_#_qux`
    echo(uri)                   // prints: /tmp/foo/bar_#_qux
    echo(uri.toFile)            // prints: /tmp/foo/bar_#_qux
    echo(uri.toFile.normalize)  // prints: file:/D:/tmp/foo/bar_
    echo(uri.toFile.osPath)     // prints: \tmp\foo\bar_
    echo(File.os(uri.toStr))    // prints: /tmp/foo/bar_\#_qux
  }
}

Please pay special attention to the last 2 echo statements.

Any ideas, please?

tcolar Wed 16 Jan 2013

Use

File.os("/tmp/foo/bar_#_qux")

In "normal" uri's (web) the # is used for anchors and not considered part of the actual path so it gets dropped probably.

ortizgiraldo Wed 16 Jan 2013

Thanks @tcolar, that worked and now I can access the files.

I'd like to add that if you need to print the path echo(File.os("/tmp/foo/bar_#_qux")) won't work and you need to invoke osPath on the file, e.g. echo(File.os("/tmp/foo/bar_#_qux").osPath) to get it right.

brian Wed 16 Jan 2013

Since we treat files as URIs, the pound char is normally treated as the fragment identifier. In a path where this is not the case, then you need to backslash escape it:

uri := `/tmp/foo/bar_\#_qux`

ortizgiraldo Thu 17 Jan 2013

Thanks @brian for the explanation. I'll keep the Str form though, just to avoid having to escape dynamically generated paths. The escaped form also gives me some unexpected results in the sample class above.

Login or Signup to reply.