Search code examples
javawindowspathseparator

Ambiguous path separator on Windows - how to handle it?


Another question brought up an interesting problem:

On Windows, the Java File.pathSeparatorChar is ;, which is correct. However, the semicolon is actually also a valid character to folder or file names. You can create a folder named Test;Test1 on Windows.

The question is: How would you determine whether the semicolon in a path list actually separates a path or is part of the directory name, if the path list can contain both absolute and relative paths?


Solution

  • If the path contains a ; itself the path must be surrounded by double quotes ".

    following small PoC

    mkdir "foo;bar"
    echo echo execute %%~dpnx0 > "foo;bar\dummy.cmd"
    set PATH=%PATH%;"foo;bar"
    dummy.cmd
    

    the output will be

    execute R:\temp\foo;bar\dummy.cmd
    

    means the dummy.cmd is found by the path setup.

    edit As to see from the comments: Using a semiclon could lead you into some trouble. It's better to avoid using directory names containing a semicolon.