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

Long path names

If a filename contains spaces you must surround it with double quotes: "my file.txt"
Filenames that include quotes or parenthesis (') are legal filenames but they can still cause problems, particularly within batch files where these characters have special meaning.

Win32 File Namespaces

The NTFS file system supports large 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. This means it is sometimes possible, when moving files and directories around, or mapping drives, to create a pathname which is too long for Win32 to process.

There is an alternative for accessing very long filenames: for file I/O, the "\\?\" prefix to a path string tells the Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.

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.

* The 260 character limit has a couple of caveats, it includes a null terminator character and every folder must be able to hold a file that is 8.3 characters long (8+3+'.' = 12 characters). This means that the longest FOLDER you can create is 260 - 12 - 1 = 247 characters.

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.

Win32 Device Namespaces

The "\\.\" prefix will access the Win32 device namespace instead of the Win32 file namespace. This is how access to physical disks and volumes is accomplished directly, without going through the file system, if the API supports this type of access.
e.g.
"\\.\COM1"
"\\.\COM56"

Short file names

Prior to Windows 95 in 1995, Windows only supported short 8.3 filenames.

Microsoft disabled the generation of short filenames (8.3) by default starting with Windows 8 / Server 2012. If they exist you can use the parameter extensions %~sp1 to expand %1 to return an 8.3 pathname.

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.

If it exists, the old short 8.3 filename can be used to bypass the 260 character pathname limit.

The order in which you create files will affect short 8.3 names
e.g.
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.

Enable or Disable 8.3 filenames in NTFS

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

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.

Fresh Windows installations now have the generation of 8.3 equivalent filenames turned OFF by default.
Disabling 8.3 filenames will provide a small improvement in directory enumeration performance.

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

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

or this can be done in the registry (see Q121007) Disable:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
NtfsDisable8dot3NameCreation=1

Enable:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem]
NtfsDisable8dot3NameCreation=0

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.

NTFS file system

Legal characters in NTFS filename include any character in the current code page, including Unicode characters, so the following are all valid:

! [ ] . ; = ( ) ☺

Illegal Characters: The following characters are not permitted in Windows file or directory names:

/ \ : * ? " < > |

Windows Explorer will not create files with a period (.) as the first or only character in the filename, although NTFS (and many command-line tools) do support this.

A long file name (LFN) can be up to 255 characters long.
NTFS supports paths up to 32768 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.

8.3 format filenames

These can have between 1 and 8 characters in the file name. Short file names have the 8.3 format and are compatible with MS-DOS and other legacy operating systems.
The name must start with a letter or a number and can contain any characters except the following:

 / \ : * ? " [ ] | = , . ; (space)

An 8.3 file name typically has a file name extension between one and three characters long with the same character restrictions. A period separates the file name from the file name extension.

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).

Host names/ Domain names / Computer Names / LDAP

Characters which are prohibited in a DNS Host name or Domain name:

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

Characters which are prohibited in a sAMAccountName or logon name, max length: 20 characters or 16 characters for computer objects. Logon names can be up to 104 characters. However, it isn't practical to use logon names that are longer than 64 characters.

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

Characters which are prohibited in a NETBIOS Computer names/Domain name (for very old versions of Windows, max length 15 characters):

\ /: * ? “ < > |

Active Directory does allow a wide range of punctuation and spaces in object names. The names of security principal objects can contain all Unicode characters except the special LDAP characters defined in RFC 2253. This list of special characters includes: a leading space; a trailing space; and any of the following characters:  # , + " \ < > ;
To allow easy scripting against Active Directory, stick to alphanumerics, dash and underscore, so Sales-Directors or SalesDirectors rather than "Sales Directors".

Characters which can be used anywhere:

A-Z, a-z, 0-9, the horizontal dash "-"

The period character, ".", is also allowed for sAMAccountName / logon name/ computer name, but it cannot be the final character in a sAMAccountName.

Reserved Names

Do not use the following reserved names for the name of a file or folder:
CON, PRN, AUX, NUL, conIN$ , conOUT$
COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9,
LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, LPT9.

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

Do not end a file or directory name with a space or a period. Although the underlying raw NTFS file system may support such names, the Windows shell and user interface do not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

“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

How-to: xlong.cmd - Find filenames that exceed the 260 char pathname limit.
How-to: Escape Characters, Delimiters and Quotes
Active Directory - Requirements for creating objects (Richard Mueller).
docs.microsoft.com - Naming conventions in Active Directory for computers, domains, sites, and OUs.


 
Copyright © 1999-2023 SS64.com
Some rights reserved