Last active
September 1, 2024 14:00
-
-
Save doctaphred/d01d05291546186941e1b7ddc02034d3 to your computer and use it in GitHub Desktop.
Invalid characters for Windows filenames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Information from https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file : | |
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 data streams where these characters are | |
allowed. For more information about file streams, see File Streams. | |
- Any other character that the target file system does not allow. | |
- Do not use the following reserved names for the name of a file: | |
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, | |
COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. | |
Also avoid these names followed immediately by an extension; for | |
example, NUL.txt is not recommended. | |
- Do not end a file or directory name with a space or a period. Although | |
the underlying file system may support such names, the Windows shell and | |
user interface does not. However, it is acceptable to specify a period | |
as the first character of a name. For example, ".temp". | |
--- | |
Handy list to copy/paste: | |
<>:"/\|?* | |
--- | |
Note: Other OSs and file systems may vary; but in general, the only forbidden characters | |
in filenames on Unix-like systems appear to be the forward slash (/) and the null byte. |
For Python3:
import re
re.sub(r'[<>:/\|?*"]+',"",filename)
Can a filename have space under windows?
Yes it can, you can verify by creating one yourself
Python3 :
def cleanFilename(sourcestring, removestring ="\"|%:/,.\\[]<>*?") :
return ''.join([c for c in sourcestring if c not in removestring])
If you know of a good GitHub Action that detect (or replace) those filenames automatically, I'm looking for one.
I'd like to rename a filename with a "-" (hyphen) suffix but windows (7) refuses.
Do you guys think this is forbidden only at the end of filenames?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For .Net, there is https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidpathchars?view=net-5.0:
name?.IndexOfAny(Path.GetInvalidFileNameChars()) == -1
orname?.IndexOfAny(Path.GetInvalidPathChars()) == -1
depending if you want to validate a file name or a full path.It will still not catch issues with file names like NUL or COM on FAT or NTFS.