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.
@petrhdk
Copy link

petrhdk commented Jan 6, 2021

function filenameValidator(fname, { replacement = "�" } = {}) {
    // https://stackoverflow.com/a/31976060
    // https://gist.github.com/doctaphred/d01d05291546186941e1b7ddc02034d3

    const fname_original = fname;

    // resolve multi-line, whitespace trimming
    fname = fname.split(/[\r\n]/).map(s => s.trim()).filter(s => s.length).join("  ");
    
    // forbidden characters
    // (after multi-line, because new-line-chars are themselves forbidden characters)
    fname = fname.replaceAll(/[<>:"\/\\\|?*\x00-\x1F]/g, replacement);

    // advanced trim
    fname = fname.replace(/\.$/, "");

    // empty filename
    if (!fname.length) {
        fname = '_';
    }
    
    // forbidden filenames
    if (fname.match(/^(CON|PRN|AUX|NUL|COM1|COM2|COM3|COM4|COM5|COM6|COM7|COM8|COM9|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|LPT7|LPT8|LPT9)(\..+)?$/)) {
        fname = `_${fname}`;
    }
    
    return {
        fname,
        isOriginal: (fname === fname_original),
    };
}

@pcrama
Copy link

pcrama commented Jan 15, 2021

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 or name?.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.

@syedsouban
Copy link

For Python3:
import re
re.sub(r'[<>:/\|?*"]+',"",filename)

@charlie39
Copy link

Can a filename have space under windows?

@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