0

I need to concatenate several absolute file paths in one string. But if I use character, that can be a part of file name or path the string will be messed. Which separator can be used?

Now I use File.pathSeparator and for Windows 7 it is ';'. But this character can be a part of file name.

SO question

Semicolons are legal in NTFS file paths.

Community
  • 1
  • 1
Yegoshin Maxim
  • 872
  • 2
  • 21
  • 54

2 Answers2

2

You can use File.pathSeparator:

The system-dependent path-separator character, represented as a string for convenience. This string contains a single character, namely pathSeparatorChar:

The system-dependent path-separator character. This field is initialized to contain the first character of the value of the system property path.separator. This character is used to separate filenames in a sequence of files given as a path list. On UNIX systems, this character is ':'; on Microsoft Windows systems it is ';'.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • I use it now. But file names in Windows 7, for example, can have ';'. – Yegoshin Maxim Mar 29 '13 at 09:44
  • 1
    Unfortunately no, while the path separator is used to separate paths there's nothing that stops you from using a colon in a file name on Unix. – Joni Mar 29 '13 at 09:45
  • @YegoshinMaxim You can check whether the path until `;` exists, if not, then you know it's part of a file name. BTW, I'm working with it for long time and never encountered a file with `;` in its name. – Maroun Mar 29 '13 at 10:07
  • To create a file called `;.:*?<>|\^` on Linux, use `touch ';.:*?<>|\^'` – Joni Mar 29 '13 at 11:03
0

The ASCII NUL character \0 cannot be a part of a pathname on Windows or Unix (including Mac), so you probably can use that as a separator.

The NUL character can't be used in a file path because it is the string terminator character in C, and the APIs for these operating systems are defined as a C libraries that use strings to represent file paths.

If you want to use a printable character as a separator you'll have to use an escape code scheme. One of the simplest is doubling the separator: suppose you use # as the separator; to add a path to the list you replace any # already present in the path with ##.

Joni
  • 108,737
  • 14
  • 143
  • 193