How-to: Long filenames, NTFS and legal filename characters

Long File/Path Names

A local path is structured in the following order: Drive letter, Colon, Backslash, Name components separated by backslashes, and a terminating null character. 1+2+256+1 = 260 characters (the Windows MAX_PATH).


The maximum filename.ext length is 256 characters, that is assuming the file is in the root of the drive.

If including the drive letter prefix, the maximum /pathname/+filename length is 259 characters.


An NTFS folder must be capable of holding an 8.3 file (8+1+3 = 12)
so the maximum folder pathname including a drive prefix is 259 - 12 = 247 characters.


If a filename or pathname contains spaces you must surround it with double quotes: "my file.txt", "\My Files\demo.txt"
The quotes do not become part of the filename.

Names that include single quotes or parenthesis are legal filenames but they can cause issues with batch files where these characters have special meaning: jan’s(special)file.txt.

To list very long paths use ROBOCOPY /L or DIR /s (xlong script), Robocopy will Tab-separate the file listing which can be useful to separate filenames from file-sizes.
To edit very long filenames, use SUBST or the \\?\UNCPATH\ syntax detailed below.

Win32 Device Namespaces

The "\\.\" prefix will access the Win32 device namespace instead of the Win32 file namespace. This allows access to physical disks and volumes is accomplished directly, (where the API supports this type of access).


The NTFS file system supports long paths and file names up to 32,767 unicode characters, normally this is restricted by the 260 character MAX_PATH limit enforced by the Windows Win32 API described above. This means it is possible, when moving files and directories around, or mapping drives, to create a pathname which is too long for Win32 to process.

The "\\?\" Literal device prefix to a path string tells the Windows APIs to disable all string normalisation and to send the string that follows it straight to the file system. This is useful for accessing very long filenames:

dir "\\?\Z:\Teams\Personnel\some - very - long - file - name.txt"

dir "\\?\UNC\Server64\Teams\Personnel\some - very - long - file - name.txt"

Because it turns off automatic expansion of the path string, the "\\?\" prefix also allows the use of ".." and "." in the path names, which can be useful if you are attempting to perform operations on a file with these otherwise reserved relative path specifiers as part of the fully qualified path. This syntax can be used in both CMD and PowerShell.

Starting in Windows 10, version 1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior.

This syntax can be used to get around many of the naming limitations inherent in Win32 (which are listed at the bottom of this page) while this can be useful to access otherwise unreadable data, it is also sometimes used to obfuscate files and directories.

See the DEL page for more on deleting very long filenames.

Normalisation - path separators

The Win32 file API has a string normalisation process which consists of: Replacing any forward slashes (/) with backslashes (\), Collapsing repeated backslash separators into one, Resolving relative paths replacing any . or .. and lastly Triming any trailing spaces and periods.

PowerShell does a better job of supporting these than CMD, so DIR C:/demo/sample.txt will work in PowerShell but not in CMD.

Type Example
Absolute Drive Path C:\demo\sample.txt
Relative to current drive/directory \demo\sample.txt
Relative to current Directory CD \demo
UNC path \\server64\demo\sample.txt
Device path (normalized) \\.\C:\demo\sample.txt
Device path (literal) \\?\C:\demo\sample.txt

Illegal Characters

The following characters are not permitted:

      Colon         quote     Pipe             Period Spaces
Windows File/Directory names
NTFS / OneDrive / SharePoint online
/ NETBIOS Computer name
/ \ :     * ? " < > |               Leading and/or trailing spaces.
(see note below)
8.3 format Filename / \ : ; , * ? "     | [ ]   =     . any space
SMB Share Name / \ : ; , * ? " < > | [ ] + =       Leading and/or trailing spaces.

sAMAccountName or logon name

/ \ : ; , * ? " < > | [ ]   = +      

DNS Host name or Domain name


{ } : ~ , @ ! ' ( )   # $ % ^ & _ . any space

Active Directory

  \   ; ,     " < >   #   +         Leading and/or trailing spaces.

Characters which can be used any filename: A-Z, a-z, 0-9 ! - ( ) + _ {  } ~

A path name may be no more than 2,048 characters in length. Individual components in the path can be a maximum of 255 characters in length.

NTFS supports paths up to 32,768 characters in length, but only when using the Unicode APIs.

When using very long path names, prefix the path with the characters \\?\ and use the Unicode versions of the C Runtime functions.

Short file names (8.3)

Prior to Windows 95 in 1995, Windows only supported short 8.3 filenames. For backwards compatibility, Windows has an option to generate an 8.3 equivalent short filename for every long filename.
If a filename is already 8.3 characters or less, this is not needed, but for a file like "My long filename.txt" it will generate a (largely hidden) second name such as MYLONG~1.TXT

Short filenames are compatible with MS-DOS and other legacy Operating Systems and have between 1 and 8 characters in the file name followed by a file name extension of between zero and three characters long.

The name must start with a letter or a number. A period separates the file name from the file name extension.

Microsoft disabled the generation of short filenames (8.3) by default starting with Windows 8 / Server 2012.

If it exists, the old short 8.3 filename can be used to bypass the 260 character pathname limit e.g. to delete a very long file.
You can use batch parameter extensions %~sp1 to expand %1 to return a full 8.3 pathname.

Unlike MS-DOS where all filenames are upper-case (and not case-sensitive), Windows allows both upper and lower case filenames, however a short filename that is automatically created for a long filename will always be in Upper Case.

The order in which you create files will affect short 8.3 names
echo abc > "a long file 1.txt"
echo abc > "a long file 3.txt"
echo abc > "a long file 2.txt"
DIR /x
:: will produce this:
ALONGF~1.TXT "a long file 1.txt"
ALONGF~3.TXT "a long file 2.txt"
ALONGF~2.TXT "a long file 3.txt"

If these files are now copied to another folder the 8.3 filenames will change, this is most likely to happen when upgrading server storage or restoring data from a backup.

Similarly for folders
md "a long folder 1"
md "a long folder 3"
md "a long folder 2"
DIR /x
:: will produce this:
ALONGF~1 "a long folder 1"
ALONGF~3 "a long folder 2"
ALONGF~2 "a long folder 3

Again copying these to somewhere else will change the 8.3 names to:
ALONGF~1 "a long folder 1"
ALONGF~2 "a long folder 2"
ALONGF~3 "a long folder 3"

See the Wildcards page for more long/short filename issues.

Some characters are invalid in 8.3 format filenames but are valid in NTFS filenames and are also valid Delimiters Typically the Windows GUI will silently change the 8.3 name for such files where necessary, for example DE=MO.TXT will become DE_MO~1.TXT

You can use long file names in both NTFS and FAT volumes (typically removable flash drives and memory cards).

Enable or Disable 8.3 filenames in NTFS

In any directory containing more than 25,000 files there is a significant performance cost ~ 6,000 files per hour vs 2 million files per hour.
Disabling 8.3 filenames will also provide a small improvement in directory enumeration performance.

Fresh Windows installations now have the generation of 8.3 equivalent filenames turned OFF by default.

If 8.3 equivalent filenames are disabled, ancient 16 bit software (like WordPerfect 3) will still be able to read/write short filenames, but won’t see a short equivalent for long filenames.

If 8.3 equivalent filenames are disabled, existing 8.3 filenames are not deleted, but moving or copying files to a different folder or creating new files will no longer automatically generate 8.3 filenames.

Disable the creation of 8.3 equivalent filenames:

FSUTIL.exe behavior set disable8dot3 1

Enable the creation of 8.3 equivalent filenames:

FSUTIL.exe behavior set disable8dot3 0

For pre Windows 7/2008 R2 machines without FSUTIL, this can be done in the registry (see Q121007)



The SQL Server FILESTREAM feature recommends that 8.3 filename support is turned OFF along with the "last access time" property.

Batch files .BAT or .CMD ?

Batch files can be saved with the extension .BAT or .CMD
The .BAT extension will run under Windows 95/MSDOS or later but the .CMD extension will only run under NT, XP or later.

One key difference between .CMD and .BAT scripts (running under CMD.EXE) is that with extensions enabled, commands like PATH/APPEND/PROMPT/SET/ASSOC will reset ERRORLEVEL to 0 if they succeed. In the old style .BAT file, the ERRORLEVEL will not be changed unless there is a new error (source).

The old behaviour where some commands will reset the ERRORLEVEL and some don’t is not considered helpful and is likely to result in buggy code - you test for one error but actually pick up a different unrelated error.

Reserved Names

Do not use the following reserved names for the name of a file or folder:
CON, PRN, AUX, NUL, conIN$ , conOUT$

Also avoid these names followed immediately by any extension; for example, NUL.txt is not permitted. Sources: [x] [x] [x]

Maximum lengths

sAMAccountName or NT4 logon name = 20 characters.
Logon name = 104 characters. However, it isn’t practical to use logon names that are longer than 64 characters.
NETBIOS Computer name = 15 characters.
DNS computer name: Minimum length = 2 characters, Maximum length = 63 characters.

“Tongues, like governments, have a natural tendency to degeneration; we have long preserved our constitution, let us make some struggles for our language” ~ Samuel Johnson: Preface to the Dictionary

Related commands

FSUTIL file - Make the file contents of a folder Case Sensitive.
How-to: xlong.cmd - Find filenames that exceed the 260 char pathname limit.
How-to: Escape Characters, Delimiters and Quotes
RFC 2253 - Special LDAP characters.
Active Directory - Requirements for creating objects (Richard Mueller). - Naming conventions in Active Directory for computers, domains, sites, and OUs.

Copyright © 1999-2024
Some rights reserved