Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a function which needs to output a string containing a list of file paths. I can choose the separation character but I cannot change the data type (e.g. I cannot return a List<string> or something like that).

Wanting to use some well-established convention, my first intuition was to use the semicolon, similar to what Windows's PATH and Java's CLASSPATH (on Windows) environment variables do:

C:\somedir\somefile.txt;C:\someotherdir\someotherfile.txt

However, I was surprised to notice that ; is a valid character in an NTFS file name.

So, is the established best practice to

  • just ignore this fact (i.e. "no sane person should use ; in a file name and if they do, it's their own fault") or
  • is there some other established character for separating Windows paths or files? (The pipe (|) might be a good choice, but I have not seen it used anywhere yet for this purpose.)
share|improve this question
    
Can you use a newline (\n) or does it have to be in a block? –  Deco Oct 24 '12 at 7:58
    
@Deco: Good idea, haven't thought of that! –  Heinzi Oct 24 '12 at 9:07
    
Another established convention is to enclose names with awkward characters in quotes, so your scanning routine can simply fetch all the chars in between without the need to check for separators. (This fails, of course, for names that contain quotes). –  Mr Lister Oct 24 '12 at 9:19

3 Answers 3

up vote 8 down vote accepted

| is one of the invalid filename/directory name characters and is frequently used as a separator, so it is a good choice for a separator of paths in a string.

share|improve this answer

Surprisingly, but |, <, and other symbols from the link provided by Oded are reserved characters only for CreateFile function and several other Windows APIs. Definitely, NTFS driver doesn't know about these restriction and allows to create file names with these symbols, which could be easily done for example in Linux using ntfs-3g driver+tools. In Windows also possible to create files with such names which would be not-deleted/readable/writable by ordinar API calls using low-level API or from kernel mode.

I suppose, the best alternative against using some esoteric separator would be using some serialization format like XML or json, which supported in many modern languages.

E.g. you data in json format will look like:

["C:\somedir\somefile.txt",C:\someotherdir\someotherfile.txt"]

share|improve this answer
1  
Fortunately, having to deal with such low-level file names is not a requirement in my case, but it's good to know nevertheless. Thanks and welcome to programmers.se! –  Heinzi Oct 24 '12 at 10:11
    
Oh, the quadruple-backslash, how we love thee... –  Yamikuronue Oct 24 '12 at 13:54
    
It's because of python default output:) Real string doesn't contain them. –  dr. Oct 24 '12 at 19:56

The windows PATH setting uses semicolons (;). Seems like that might be a good standard to follow.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.