views:

491

answers:

5

I'm looking for a character to use a filename delimiter (I'm storing multiple filenames in a plaintext string). Windows seems not to allow :, ?, *, <, >, ", |, / and \ in filenames. Obviously, \ and / can't be used, since they mean something within a path. Is there any reason why any of those others shouldn't be used? I'm just thinking that, similar to / or \, those other disallowed characters may have special meaning that I shouldn't assume won't be in path names. Of those other 7 characters, are any definitely safe or definitely unsafe to use for this purpose?

A: 

It is actually possible to create files programmatically with every possible character except \. (At least, this was true at one time and it's possible that Windows has changed its policy since.) Naturally, files containing certain characters will be harder to work with than others.

What were you using to determine which characters Windows allows?

Update: The set of characters allowed by Windows is also be determined by the underlying filesystem, and other factors. There is a blog entry on MSDN that explains this in more detail.

Greg Hewgill
If you try to rename a file in Windows Explorer, it comes up with that list, and stops you from doing it. I just assumed that it was like that at a fundamental level - but you're saying it's not so?
Smashery
There are a lot of layers below Explorer. :) I've updated my answer with more info.
Greg Hewgill
Doesn't : separate a file into different forks...name:text, name:data, etc? And how do you get a slash into a file name on Windows? I am under the illusion that it treats slash and backslash as equivalent at the system call level.
Jonathan Leffler
See: http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29 for information on alternate data streams (which I called 'forks').
Jonathan Leffler
NTFS alternate data streams are (not surprisingly) an NTFS feature. The valid set of characters in filenames on Windows *depends on the underlying filesystem and subsystem*. I suspect you can create a file with a `:` in the name using the Posix subsystem.
Greg Hewgill
+3  A: 

The characters : and " are also used in paths. Colon is the drive unit delimiter, and quotation marks are used when spaces are part of a folder or file name.

The charactes * and ? are used as wildcards when searching for files.

The characters < and > are used for redirecting an applicartions input and output to and from a file.

The character | is used for piping output from one application into input of another application.

I would choose the pipe character for separating file names. It's not used in paths, and it's shape has a natural separation quality to it.

An alternative could be to use XML in the string. There is a bit of overhead and some characters need encoding, but the advantage is that it can handle any characters and the format is self explanatory and well defined.

Guffa
`"`is not allowed in a path: it is considered a delimiter which surrounds a path when it contains special characters, but it is not part of the path.
Adrien Plisson
@Adrien: It certainly is. It can either be used around the entire path or around an element in a path, like C:\"Program Files"\Adobe.
Guffa
@guffa: i do insist, is is not. double-qoutes are used as a mean to escape special character, spaces to be precise. they are specially interpreted by Windows shell and command prompt, but are not part of the filename at all. can you show me a way to create a file whose name contains double quotes, using either the command prompt or Windows API ?
Adrien Plisson
@Adrien: A file name does not contain quoation marks, and I have never said that they do. A path can contain quotation marks used to specify the components of the path, just like colon and backslashes are used in a path to separate the components although a file name never contains a colon or a backslash.
Guffa
well, sorry for the stubbornness, i never thought of it this way but you are definitely right.
Adrien Plisson
A: 

Why dont you use any character with ALT key combination like ‡ (Alt + 0135) as delimiter ?

Ashish
What character code would that be?
Thomas Padron-McCarthy
That's a legal character in filenames and cannot be used as a seperator for that reason.
MSalters
+1  A: 

I have used * in the past. The reason for portability to Linux/Unix. True, technically it can be used on those fileysystems too. In practice, all common OSes use it as a wildcard, thus it's quite uncommon in filenames. Also, people are not surprised if programs do break when you put a * in a filename.

MSalters
+3  A: 

Windows uses the semicolon as a filename delimiter: ;. look at the PATH environment variable, it is filled with ; between path elements.

(Also, in Python, the os.path.pathsep returns ";", while it expands to ":" on Unix)

Adrien Plisson
Strange, though, that I can create files with semicolons in them.
Smashery
that's strange, but that's the way it is... when you add a path which contains a ´;´ to the %PATH%, the added path is surrounded by `"` (which is not allowed in a path). maybe surrounding filenames with `"` and separating them with `;` is your solution.
Adrien Plisson