Skip to content

Instantly share code, notes, and snippets.

@doctaphred
Last active March 9, 2024 21:03
Show Gist options
  • Star 68 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save doctaphred/d01d05291546186941e1b7ddc02034d3 to your computer and use it in GitHub Desktop.
Save doctaphred/d01d05291546186941e1b7ddc02034d3 to your computer and use it in GitHub Desktop.
Invalid characters for Windows filenames
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.
@syedsouban
Copy link

Yes it can, you can verify by creating one yourself

@haiueom
Copy link

haiueom commented Oct 22, 2022

Python3 :

def cleanFilename(sourcestring,  removestring ="\"|%:/,.\\[]<>*?") :
    return ''.join([c for c in sourcestring if c not in removestring])

@DNA-PC
Copy link

DNA-PC commented Oct 12, 2023

If you know of a good GitHub Action that detect (or replace) those filenames automatically, I'm looking for one.

@ychaouche
Copy link

ychaouche commented Feb 8, 2024

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