Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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.
@sagi053

This comment has been minimized.

Copy link

@sagi053 sagi053 commented Mar 24, 2020

Nice regex to find and replace invalid chars in file name.
[<>:"/\|?*]
example:
javascript:
"my file is * invalid ?.pdf".replace(/[<>:"/\|?*]/g,"");
php:
$fileName = preg_replace('/[<>:"/\|?*]/','
','my file is * invalid ?.pdf');
c#
var fileName = (new Regex(@"[<>:""/\|?*]")).Replace("my file is * invalid ?.pdf","_");

@doctaphred

This comment has been minimized.

Copy link
Owner Author

@doctaphred doctaphred commented Mar 25, 2020

Thanks for the contribution! Couple of notes:

\ is the escape character in most regex engines, so you'll need to repeat it to make sure it gets included in the character class and doesn't just escape the | after it: [<>:"/\\|?*] / "my file is \\ invalid ?.pdf".replace(/[<>:"/\\|?*]/g, "_");

Also, I'm not super confident in my PHP knowledge, but I think you'll need to double-escape the backslash: once because PHP treats it as an escape character in the string literal (even when using single quotes), and a second time for the regex engine. So I think you'll need a total of four \ characters: '/[<>:"/\\\\|?*]/' (gross).

(It looks like C# uses the @ prefix to denote verbatim strings, which look like Python's raw strings, and should only need a single escape for the regex engine. JS does not (yet) seem to offer unescaped string literals, but RegExp literals don't apply the additional layer of escaping.)

@Legorooj

This comment has been minimized.

Copy link

@Legorooj Legorooj commented Apr 6, 2020

@doctaphred

This comment has been minimized.

Copy link
Owner Author

@doctaphred doctaphred commented Apr 6, 2020

Thanks! I updated the URL 👍

@vollmann-ariel

This comment has been minimized.

Copy link

@vollmann-ariel vollmann-ariel commented Apr 28, 2020

Thanks, dude!

@defrindr

This comment has been minimized.

Copy link

@defrindr defrindr commented Jun 22, 2020

its very nice ,sir

@AnastasiaDunbar

This comment has been minimized.

Copy link

@AnastasiaDunbar AnastasiaDunbar commented Jun 30, 2020

JavaScript:

let toFilename=string=>string.replace(/\n/g," ").replace(/[<>:"/\\|?*\x00-\x1F]| +$/g,"").replace(/^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])$/,x=>x+"_");
@ivan

This comment has been minimized.

Copy link

@ivan ivan commented Nov 21, 2020

Microsoft's documentation neglects to mention COM0 and LPT0 which explorer.exe has trouble with (even on Windows 10 20H2), possibly because of a bug.

@pitizzzle

This comment has been minimized.

Copy link

@pitizzzle pitizzzle 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

This comment has been minimized.

Copy link

@pcrama 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment