8

One of my users asked why my application does not support semicolons in filenames. I stepped through my code, and it seems Windows function GetOpenFileName truncates any filename containing a semicolon. E.g., "one;two.wav" -> "one".

Microsoft says colons are not allowed, but it doesn't mention semicolons...

Naming Files, Paths, and Namespaces

Are they legal or not?

And how can I get GetOpenFileName() to work with semicolons in a filename?

OH! Weird; the filename is correct, except 'scrolled' off to the left. So "one;two.wav" looks like "two.wav" until I click it and press left-arrow (then it's fine). So it's not a bug as such, only weird behaviour.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeff
  • 402
  • 1
  • 6
  • 14

5 Answers5

11

Semicolons are legal in NTFS file paths.

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

The following reserved characters:

  • < (less than)
  • > (greater than)
  • : (colon)
  • " (double quote)
  • / (forward slash)
  • \ (backslash)
  • | (vertical bar or pipe)
  • ? (question mark)
  • * (asterisk)
  • Integer value zero, sometimes referred to as the ASCII NUL character.
  • Characters whose integer representations are in the range from 1 through 31, except for alternate streams where these characters are allowed.
  • Any other character that the target file system does not allow.

I'm able to add semicolons to filenames on my Windows 7 system. Watch for code, probably yours or third-party code, that does strange things with unexpected characters (most notably spaces).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • Actually, colons (`:`) are reserved for NTFS alternate data streams. You can therefore do this in a command window: `echo NUL > foo` then `echo hello > foo:bar`. – Benoit Oct 06 '10 at 09:00
  • 1
    @Benoit: But Michael did not state otherwise? – Sebastian Mach Mar 26 '12 at 10:05
  • 3
    Another indicator that Microsoft has absolutely no idea what the frig they're doing. PATH uses semicolon as the separator between individual paths, which obviously breaks if it ever contains any entries with semicolons in the path name. – antred Oct 08 '21 at 12:57
  • Also there are a lot of reserved names: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9 – XuMuK Apr 21 '22 at 09:23
4

Though it may be omitted in the Windows handbooks, the semicolon is a reserved character too, for example "dir .dat;.bak" is a legal command. The same applies to the plus character, for example "copy test1.dat+test2.dat test3.dat" is a legal command.

Vertilizer
  • 41
  • 1
  • Do you have a reference for it? From [Reserved characters and words, In Windows](https://en.wikipedia.org/wiki/Filename#In_Windows): *"semicolon. Allowed, but treated as separator by the command line interpreters COMMAND.COM and CMD.EXE on DOS and Windows."* – Peter Mortensen May 05 '23 at 22:12
  • The question is about Windows filenames, not the Windows command line (e.g., [cmd.exe](https://en.wikipedia.org/wiki/Cmd.exe)) – Peter Mortensen May 05 '23 at 22:14
  • OK, the OP has probably left the building: *"Unregistered. Member for 10 years, 11 months"*. But theoretically the cookie could have survived(?). – Peter Mortensen May 05 '23 at 22:14
2

True: Windows allows a semicolon in file names. But when you burn such files to a data CD or DVD disc, the names get truncated. This I experienced when using aHead Nero version 9.

Henk
  • 21
  • 1
  • [ISO 9660](https://en.wikipedia.org/wiki/ISO_9660)? Perhaps *"All levels restrict file names in the mandatory file hierarchy to upper case letters, digits, underscores ("_"), and a dot."*? What about extension to ISO 9660? What were the details? – Peter Mortensen May 05 '23 at 22:05
  • OK, the OP has probably left the building: *"Unregistered. Member for 10 years, 11 months"*. But theoretically the cookie could have survived(?). – Peter Mortensen May 05 '23 at 22:09
1

Yes, they are allowed. Just that if you are running them in the command line you have to put quotes within them.

drhanlau
  • 2,517
  • 2
  • 24
  • 42
1

Yes. A semi-colon is a legal character in a Windows file-name. It wouldn't surprise me, though, if there were other programs that had a problem with them.

Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116