6

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?

Community
  • 1
  • 1
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139

3 Answers3

5

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.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Ahaa! Now we're talking. This seems reasonable. – Thorsten Dittmar Apr 14 '15 at 09:50
  • Interesting, this works, but when I tried `which dummy` it didn't find it. – Always Learning Apr 14 '15 at 09:53
  • You mean `where dummy`? – Thorsten Dittmar Apr 14 '15 at 09:56
  • @stvcisco If you mean the Unix tools `which` it should be `which dummy.cmd`. But it's not found. As `where dummy.cmd` it also didn't find the file I would not blame `which` for that. ;-) – SubOptimal Apr 14 '15 at 10:01
  • @ThorstenDittmar Thanks for the `where` command (didn't know that till now). Based on the fact that this is also not able to handle this case. It seems either `;` shouldn't be allowed or the handling of directories this broken some how. – SubOptimal Apr 14 '15 at 10:04
  • 1
    While this does appear to work, and is (presumably) the correct way to parse such a PATH, I would strongly recommend against adding directory names that contain semicolons to PATH on production systems. – Harry Johnston Apr 15 '15 at 01:30
  • @HarryJohnston I'm fully agree with you. I add a note to my answer, so nobody can see he was not warned before. ;-) – SubOptimal Apr 15 '15 at 06:59
1

Since the question is for Java, and based on @SubOptimal answer that explains that paths with a semicolon should be enclosed in quotes, here's a small code sample to extract paths from such a list separated by File.pathSeparator:

String separatedList  = "\"test;test1\";c:\\windows;\"test2\";test3;;test4";

String pattern = String.format("(?:(?:\"([^\"]*)\")|([^%1$s]+))%1$s?", File.pathSeparator);
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(separatedList);
while (m.find())
{
    for (int i = 1; i <= m.groupCount(); i++)
    {
        String path = m.group(i);
        if (path != null)
            System.out.println(path);
    }
}

For reference, the regex without the escaping characters is (?:(?:"([^"]*)")|([^;]+));?.

Community
  • 1
  • 1
Métoule
  • 13,062
  • 2
  • 56
  • 84
0

In a Windows PATH the semicolon is always a separator. If you have a folder with a semicolon in the name, you can put its short alternate name in the PATH. To find the short name use DIR /X. For example:

C:\> dir test* /X
<DIR>   **TEST_T~1**     Test;Test1
C:\> set PATH=TEST_T~1;%PATH%
Always Learning
  • 5,510
  • 2
  • 17
  • 34
  • While I could do that, it really defeats the purpose of allowing long path names in the first place, doesn't it? – Thorsten Dittmar Apr 14 '15 at 09:48
  • The issue isn't the length of the name, but that it contains the path separator. If you want the `PATH` to interpret your folder as a folder you need to use a form that doesn't contain the separator. – Always Learning Apr 14 '15 at 09:51