As long as your file names meet operating system requirements, you can use whatever you like; the rest is up to you


A customer had a question about the MSDN documentation on rules for legal file names:

My employees keep naming documents with hyphens in the name. For example, they might name a file Budget-2012-Final.xlsx. It is my position that hyphens should not be used in this way, and the document should be named Budget 2012 Final.xlsx. Please advise on the use of hyphens within file names.

Hyphens inside file names are legal, and you can use as many as you like, subject to the other rules for file names.

If you are having an argument with your employees about file naming conventions, that’s something you just need to work out among yourselves. Whatever you decide, the file system will be there for you.

Comments (64)
  1. pc says:

    If I made the rules, the file system would disallow any name with "Final" in it. :)

  2. Distant memory says:

    However, as I remember Sharepoint doesn't follow the same rules so when I tried to bulk load some users' documentaion folders into SharePoint I had problems with &'s, all understandable of course but confusing to teh end user. support.microsoft.com/…/2933738

  3. Paul says:

    Is that language sensitive, pc? LOL.

  4. Linnaea Mutex says:

    @pc

    And we won't have final exams anymore, cause they can't name it "final exam.docx" ;)

    But we also won't have Final Fantasy anymore. :(

  5. Adam Rosenfield says:

    @pc Don't forget localization!  In order to properly localize your feature, you'll also need to make sure to ban finale, slutlig, poslední, סופי, and many more.

  6. 640k says:

    You should not use leading spaces in file names, Explorer cannot copy such files. It is very sneaky when Explorer (a part of the OS!) renames such files deep in folder trees when copying folders.

  7. David Crowell says:

    @PC,

    Can we just switch it around, and require every filename to contain final?

    system32-final.dll   (really – the only one you'll ever need)

  8. Zippy says:

    "Send to compressed file" does only support a subset of the available characters. Don't use them.

  9. Zippy says:

    –> For your own sanity, only use characters which explorer allow in compressed files.

  10. Zarat says:

    The linked MSDN page lists as forbidden characters "Any other character that the target file system does not allow."

    That makes the documentation kinda open-ended, in particular since I never found the documentation for the allowed characters of the individual file systems, anyone got a link to these?

  11. Joshua says:

    @Zarat: They're mostly remote filesystems and are probably reported as FAT32 or NTFS even though they aren't.

  12. kktkkr says:

    New naming convention: hyphens in filenames should be "escaped" by doubling them, and similarly "final" in filenames must occur in pairs.

  13. morlamweb says:

    I'm surprised that this query reached your level, Raymond.  It's actually quite easy to find the file name rules in Windows, and I don't even need the MSDN: select a file in Explorer.  Press F2.  Press the / key.  You'll get a tooltip with a list of the prohibited characters for file names.  Hyphens aren't in that list, so therefore, they're allowed in file names.  Sure, there are different rules for Windows vs. NTFS vs FAT vs. other file systems, but such a detailed answer need not be sent for such a frivolous query.

    [Oh, they know what characters are legal in file names. In fact, they referenced the MSDN page in their question. But they weren't asking about what file names are legal. They wanted to know what file names are right. -Raymond]
  14. Joshua says:

    Hmmm what are you supposed to do if FindFirstFile or FindNextFile return an illegal file name?

    C:>dir c:development?

    Volume in drive C has no label.

    Volume Serial Number is DD00-0008

    Directory of c:development

    06/03/2014  08:10    <DIR>          .

    06/03/2014  08:10    <DIR>          ..

    06/03/2014  08:11                 3 ?

                  1 File(s)              3 bytes

                  2 Dir(s)  765,933,686,784 bytes free

    C:>

  15. alegr1 says:

    @Joshua:

    Use \?

  16. 12BitSlab says:

    I am guessing that the customer has some internally developed  application(s) that can't deal with hyphens in filenames.  Just a guess…

  17. icabod says:

    @640k: Are you sure? Because I just created a file with a space at the beginning of the name, and Explorer worked with it perfectly well. Copied it around no problems. And this is on XP!

    The only issue I can forsee would be with file associations that don't "quote" their parameters, but that's an issue wherever you stick your spaces.

  18. j b says:

    My big frustration is when (some of) my colleagues insist on naming files like name_of_the_file.txt, just in case that file will some day be moved to another system (guess which one!) where lots of tools are not very good at handling file names containing spaces.

    Or, (the same group of people) insisting that we consistently refer to the file by the "correct" casing, otherwise some people (and tools that they insist on using, imported from other OSes) might consider Name_Of_File and Name_of_file two different files. So, just to be on the safe side, they try to insist on honoring casing in ways that the file system we use certainly could care less about!

  19. MNGoldenEagle says:

    @icabod, @640k: It looks like (on Windows 7 at least) moving or copying a file with any spaces at the beginning of the name get trimmed off.  It also does this when you attempt to create a file or directory with a space at the beginning (though this is more understandable).  This is likely an Explorer issue and not an issue with Win32, and even then it's pretty harmless.  Files with spaces at the beginning are likely created by users and typically are unintentional.  I can't imagine a program actually depending on a file created with such a filename, and even in such a case there are workarounds available: command prompt, PowerShell, VBScript, etc.

  20. Erik F says:

    @Zippy: Just because a ZIP file is a file doesn't make it any less a file system!

    There's at least one file system type that has fairly extreme limitations, and it's not even a remote file system: ISO 9660-formatted CDs (see en.wikipedia.org/…/ISO_9660), so you have to be careful there.

    On the *nix front, Samba does a pretty good job of mangling file names so that they don't have any illegal characters in them. Pity that the mangling turns them into completely opaque forms, though (like "NP29A6", etc.) It reminds me of when FAT long file names get corrupted.

    For Raymond's original customer: whatever you choose, be consistent about it. There is also a new-fangled feature called a "directory" that can assist you in grouping like files together.

  21. John Elliott says:

    Maybe the customer once used a DOS program which used '-' as the option lead-in rather than the SWITCHAR, and which had no way to handle hyphens in filenames?

  22. morlamweb says:

    @12BitSlab: The phrasing "It is my position…" in the customer query suggests that what follows is the person's own opinion and not a business app requirement.  If one of their applications required filenames without hyphens, then why wouldn't they have stated it?  And what app can handle spaces, but not hyphens?  The question sounds more like someone trying to use Microsoft to settle an internal dispute over file naming conventions.

  23. Adam V says:

    @morlamweb – I think you're right. Their thinking was probably "if only the operating system prevented us from using hyphens, we'd stop having this argument (and I would get my way)".

  24. Jerome Viveiros says:

    I'd outlaw any files that contain the phrase, "free", and remove all websites that promise anything for free too.

    @Rob K: Iabsolutelyagreeandwouldgoevenfurthertosaywhitespaceshouldberemovedfromcommentsaswell!

  25. Dan Bugglin says:

    @Zarat the APIs you're reading on don't care… they just pass the filenames to the underlying filesystem drivers, which will then accept or reject them based on their own rules.

    @Joshua Based on the APIs used it is possible to construct files with illegal names… if you can construct an API call using \?C:whatever as the other guy said, you can reference that file successfully.  Most likely you want to use del, which works.

    Example, try this:

    > echo. > \?%CD%con

    > dir

    > del con

    The filename, directory name, or volume label syntax is incorrect.

    > del \?%CD%con

    > dir

  26. Ace says:

    I'm just going to use en dashes and em dashes instead.

  27. Gabe says:

    This reminds me of my favorite web mail system that was no doubt written by people using a system whose filenames generally do not have spaces. Whenever you download an attachment, the spaces in its name get turned into underscores, so mailing a file to yourself changes its name.

    To avoid ending up with two copies of any particular file I just use dashes or underscores instead of spaces.

  28. alexcohn says:

    @Gabe: email attachments are kindof special. Nobody wants to download from a spoofed email a file named "innocent.jpg        .exe" with predictable side-effects.

  29. Nico says:

    An interesting bit of trivia about hyphens in Windows filenames is that leading hyphens are ignored by Explorer when sorting files.  For example, given these files sorted by  file name:

    alphabet.txt

    ponies.jpg

    resume.doc

    taxes.pdf

    If you rename "resume.doc" to "-resume.doc", you end up with:

    alphabet.txt

    ponies.jpg

    -resume.doc

    taxes.pdf

    Same goes for prepending multiple hyphens.  This can be a handy trick when you're "disabling" a file by renaming it.

    (Side note: It's quite sad that Michael Kaplan's blog was completely removed. It was a treasure trove that shouldn't have been so readily discarded by Microsoft, and that action makes me fear for the potential eventual loss of Raymond's blog).

    [Explorer is not ignoring leading hyphens. CompareString is ignoring leading hyphens. (More accurately, punctuation has lower weight. It is used to break ties.) -Raymond]
  30. Mark (The Other Mark) says:

    I prefer Budget 2012 Finalv2-new DO NO USE.xlsx

  31. j b says:

    Nicolas: "leading hyphens are ignored by Explorer when sorting files"

    Have you ever met a non-computer guy who lets any sort of punctuation affect sorting?

    By "non-cuputer guy" I mean anyone who gives you a blank stare if you mention "ASCII".

    (By 'punctuation' I do NOT mean letter _accents_, which in many languages are essential for sorting. Hyphnes, commas, full stops, colons… are not letter accents.

  32. nil says:

    I want to be able to use nulls in file names. Nulls can be saved on disk, why not allow them?

  33. Damien says:

    Well, obviously what you *should* have done is change the rules within Windows to the customer's *obviously* superior opinion on what should and should not be valid in filenames.

    I can't believe I had to point that out to you :-)

  34. Joshua says:

    @nil: Do you want to write your own OS? There are only two characters that strongly resist being used in filenames: NUL (always byte 0) and KERNEL_DIRECTORY_SEPARATOR (usually forward slash).

  35. Silly says:

    @nil: just use the Yen symbol instead and write a layer over top that replaces it at runtime with NULL. auto fileHandle = CreateFile(L"¥.txt", GENERIC_WRITE, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, 0, 0);

  36. Rob K says:

    Whitespace in a file name is *never* right.

  37. Paul Coddington says:

    If only .NET were a legal file name (in Windows Explorer that is, the file system is OK with it) and there was a layer that allowed file names to be seamlessly natural language (or perhaps special Unicode characters defined for use in file names for forbidden characters without resorting to double-width and other kludges). Seriously: there is a need for question marks, quotes, colons and slashes to exist in file names in the real world.

    But at least the customer is naming the files – you'd be amazed at the Government departments out there with network drives full of critical data named "New Document 1", "Copy of New Document 2" and variants thereof.

  38. Paul Coddington says:

    ""Send to compressed file" does only support a subset of the available characters. Don't use them."

    Yes – painfully, it rejects Japanese file names even through the rest of the file system is happy with them.

  39. Muzer_ says:

    I remember getting really confused as to why mail merge didn't work once.

    Turns out Excel isn't happy with exclamation marks in filenames. Oops.

  40. Muzer_ says:

    @Joshua Not if you base your OS around Pascal strings. Which probably wouldn't be a bad idea.

  41. Cheong says:

    Maybe the customer confused with the "Hostname must not contain underscore" limitation? (Underscore is valid for filenames by the way)

  42. j b says:

    Rob K: "Whitespace in a file name is *never* right."

    Exactly the feeling I have about personal names. Noone should be allowed to have a name containing a space.

    Same with reports and messages and street addresses and the like: No identifying tag should be allowed to contain whitespace or any other character creating problems for your favorite software and file system.

    To the h** with all cultural and social conventions! If people (and places, and street names, and …) want to take advantage of the modern computerized world, they just have to adopt to OSes and file systems that have trouble with whitespace. There is no way to support progress, except by dropping such silly expectations ss identifiers consisting of multiple words/names!

  43. Joshua says:

    @Paul Coddington: See my post. *nix allows everything but NUL and / (which can bite hard). As for Japanese, etc., no problem after the adaption of UTF-8. The old Japanese encoding is a bad idea for lots of reasons.

    Yes it bites hard when somebody puts a newline in a filename. Imagine that.

  44. Cheong says:

    @Joshua: Given in the old Japanese systems they replace "" with "¥" in their locale, it'd probably be bad idea for them to include the "Yen sign" in filename, though.

  45. Cheong says:

    [Off Topic]: Since SIAO link in Blogroll is dead, will you help to update the link to SIAO2 at http://www.siao2.com ?

  46. 640k says:

    @MNGoldenEagle: Automatically renaming files when copying is NOT harmless. It could actually be a security breach. The result is unpredictable.

  47. Matt says:

    >>> "@Joshua Not if you base your OS around Pascal strings. Which probably wouldn't be a bad idea."

    You mean like Windows does? It's all UNICODE_STRING under the hood.

  48. John Doe says:

    This is asking for it…

    What do you thing of my filename?

    C̞̈o̷̫̚p̬̎y̢̒ ̰̐o̼̅f̔ͅ ͓̽N̖̐e̲̅w̧̃ ̸̿͟D̥̾o͍͗c̜̿u̳̒m̶͢e̫̎n̴̈t̰̃ ͉̉1ͥ.͇d̤̈o̳̿c̜͑xͯ

  49. Erik F says:

    Thinking about it, there probably is a way to enforce the "no new files with hyphens" rule: create and install a file system filter driver. That way nothing can make such a file, not even the OS itself! Of course, it would break all sorts of programs but the customer is always right, correct? ;-)

    No really, don't do that. Such things are evil.

  50. MNGoldenEagle says:

    @640k: What's triggering this security breach?  Filename collisions?  Explorer still captures those and prompts you about them.  Changing a potential DLL name?  Sure, but the DLL is still loadable regardless of whether it had the space in front of it or not, and this action is entirely user-driven inside an Explorer window; it's not a behavior of Win32 itself.  It could cause programs to break if they, for some inexplicable reason, have a DLL named with spaces in the front, but that's not a security breach, that's user error.

    A malicious program could theoretically use UI automation to take advantage of this behavior, but even so, if that's happened you're already screwed because hey, a malicious program has full access to your UI!

  51. Nicholas says:

    > Explorer is not ignoring leading hyphens. CompareString is ignoring leading hyphens. (More accurately, punctuation has lower weight. It is used to break ties.)

    Thanks for the clarification, Raymond.

  52. Andreas Rejbrand says:

    I occasionally want to use EN DASH (U+2013) in file names (like "Cayley–Hamilton theorem.docx"), but so far I have always decided not to because it might cause minor annoyances in some rare situations (like when I am forced to enter the file name manually in a text field which does not feature insertion of arbitrary Unicode characters via their code points — then I have to copy and paste the character, which is awkward).

  53. Malcolm says:

    @Paul Coddington

    """Send to compressed file" does only support a subset of the available characters. Don't use them."

    Yes – painfully, it rejects Japanese file names even through the rest of the file system is happy with them."

    No problem here with Japanese names for files and folders, e.g. 新しいフォルダー or 新しいテキスト ドキュメント.txt, with the standard "Send to" (in Japanese).

  54. Martin Ba. _ says:

    Great fun can be had by putting semicolons into folder names and then trying to put said folders into the PATH environment variable. :-)

  55. Fleet Command says:

    Seriously, Mr. Chen, you bothered writing a blog post about it?

    If you can't make more than $100 out of bothering, then employ the bliss of forgetting immediately after replying.

  56. How rude... says:

    @Fleet Command, come on, lighten up mate! Not everything in life is 0 or 1.

    What you just said applies to yourself.  By hypothesis:

    - "If you can't make $100 more out of reading Raymond's blog or replying with such condescending tone without adding anything, please forget about it immediately, stop reading and don't even reply"

    Now, don't take it personally mate, since I'm not actually going to tell this to you. It was just for the purpose of explanation. I'm sure you'd like your own guests to be a bit more comprehensive and… educated.

  57. Fleet Command says:

    One thing is for certain: I won't post a single comment in your blog. (Straw man, false dichotomy and failing to assume good faith in one message?)

    But reading The Old New Thing, in general, is definitely worth more than $100.

  58. Marc K says:

    @j b: Having been bitten enough myself by case-sensitivity, I can understand where your colleagues are coming from on that point (to a degree).

  59. 640k says:

    @MNGoldenEagle:

    1.

    No, in most windows os (at least three I've tested) explorer.exe (an OS component) does not prompt the user when copying a folder and a file with leading spaces is automatically renamed.

    2.

    One potential security vulnerability is when copying a web site which is configured to prevent downloading of a file with leading spaces. The copy of the web site will have a renamed file, and it will be possible for users to download the file because the configured file name will not match. This might be an uncommon problem, but the real problem is that the result of the copy operation is unpredicatble, and thus there might exist infinite number of unknown security vulnerabilities.

  60. MikeFitz says:

    I have a file name "C# Help.chm"

    Guess what? the file system sees the "#" as an anchor tag.

    I had to rename it "C Sharp Help.chm""

  61. John Doe says:

    @MikeFitz, that's not the file system, it's whatever application(s) you're using, I suppose it includes at least a browser, and the lack of URL escaping.

  62. Fleet Command says:

    That's nothing.

    I had a file called "Delete or move me, if you can! ". With a space character at the end. I had to boot into Linux to delete it.

  63. Mike Fitz says:

    @John Doe: That's true, the file system had no problem at all. It's the consequences of the file using a # in the name when the compiled help app tried to open the file.

    Another issue I've seen is if the file is a device name, like COM1 using //?/

    Good luck deleting that if you don't know the obscure syntax!

  64. ender says:

    @Fleet Command:

    You can delete such files from command line, by using full path prefixed with \?

Comments are closed.