6

I try to understand what is the maximum valid length of the path a file.

For this, I use the default Windows file manager, on Windows 7, and the following PowerShell script:

Get-ChildItem | Select Name, FullName, @{N="Path Length";E={$_.FullName.Length}} | Format-List

I open the manager, create the file abc.txt in C:\, and then add letters to abc as long as the file manager allows it. (Actually, I use copy-and-paste, of course. This is much faster.)

Then I do the same test in C:\aaa and C:\aaa\bbb.

For some reason, the results are different. The maximum length in C:\aaa and C:\aaa\bbb is 259 characters, but the maximum length in C:\ is 258 characters. Why?

C:\abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefgh.txt
258 characters

C:\aaa\abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcde.txt 259 characters

C:\aaa\bbb\abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_a.txt 259 characters

update

From mail-archive.com, Python list:

(https://www.mail-archive.com/python-list@python.org/msg444514.html)

the classic DOS path length limits (e.g. 247, 258, or 259 characters depending on the context).

The below explanations are mine.

247 stands for the max length of the path of a directory: 260 - 12 - 1 = 247. Here, 12 stands for 8.3 filename and 1 stands for a NUL terminator. For example, C:\foo or C:\foo\bar

259 stands for the max length of the path of a file (not directory) located not in the root of the drive. For example, C:\foo\aaa.txt

258 stands for the max length of the path of a file (not directory) located in the root of the drive. For example, C:\aaa.txt

So there are actually 3 limits: 247, 258, and 259.

But why do we have 258 limit for files in the root of the drive and 259 for files in other dirs?

See also: https://www.mail-archive.com/python-dev@python.org/msg106171.html

MAX_PATH, which limits file paths to a paltry 259 characters (sans the terminating null); the current directory to 258 characters (sans a trailing backslash and null); and the path of a new directory to 247 characters (subtract 12 from 259 to leave space for an 8.3 filename).

However, the second quote doesn't make sense to me. I cannot understand why this guy talks about a trailing backslash. The trailing backslash may be a case when we talk about directories, but not files!

john c. j.
  • 314
  • 6
  • 25

3 Answers3

4

I have a simple explanation for the difference : Bad code in Windows 7, rewritten anew in Windows 10. I will show below why I think so and what are the rules on the lengths of file-names in both Windows 7 and 10.

But first, a small remark: The 260 limit of MAX_PATH is an artifact of a much earlier Windows version. It cannot be changed, because Windows API uses it heavily in its data structures, for example in WIN32_FIND_DATA, so that increasing it in the API would cause memory overrun in existing applications. That's why longer file-names have to be allowed explicitly in the registry and a program needs to declare its capability to handle long names in its manifest.

I also note that the drive-letter (C:\) is only included in the text and does not relate to the Windows disk tables (MFT). Windows knows well what disk the file resides on.

Below are my tests in Windows 7, where my findings corroborate those of the poster:

enter image description here

The following rules can be derived:

  • The Drive letter does not enter into the limit calculations (C:)
  • The leading back-slash does not enter into the limit calculations (C:\)
  • The rest is limited to 256 characters, which includes intermediate back-slashes
  • A root file can go up to 255 characters, which files in folders cannot do.

These senseless results point strongly in the direction of badly-written code.

For starters, as far as the MFT is concerned, each path component is separate and has exactly the same limits of length as any other component (sub-folder), so there is no reason that is inherent to the disk tables for limiting the length of file-names just because they are included in some folder.

Second, we have the question of why is a root file limited to 255 characters, when its seems like that it should be 256, as it is the only element in its path (which we saw can reach 256 characters).

In the search for explanation, I theorized that 255 is the real limit for file-names that was decided by Microsoft, and the rest is really only badly-written code.

To verify this theory I tested to see the behavior of Windows 10. I have repeated below the same operations (Administrator permissions needed for writing to C:\):

enter image description here

As seen above, the behavior here is much more logical: Intermediate components of the path no longer count to limit the size of the file-name, which is always 255 characters.

It became clear that it was Microsoft's intention all along to allow file-names of 255 characters, but this was disabled in Windows 7 by code that took into account the path, and for no real good reason.

Further digging discovered the documentation for the NTFS Attribute - $FILE_NAME (0x30), which specifies one byte for the length of the file-name (at offset 0x40). This explains well the 255 characters limit. (See also FireEye Part 2: The Internal Structures of a File Name Attribute).

Small anecdote: According to the Microsoft law of conservation of bugs, the Windows 10 Explorer was incapable of deleting the 255-characters files in C:\, C:\Temp and C:\Temp\abc. I had to enter in the Command Prompt the command del 12* to get rid of them. (Fix one bug while introducing another...)

harrymc
  • 498,455
2

For some reason, the results are different. The maximum length in C:\aaa and C:\aaa\bbb is 259 characters, but the maximum length in C:\ is 258 characters. Why?

I think what you are asking is what is the maximum length of a path that is typically allowed by Windows Explorer. It’s 260 characters.

That allows for 248 characters for the path plus the filename which allows for 12 characters.

The names of your example files are different due to the null character.

In editions of Windows before Windows 10 version 1607, the maximum length for a path is MAX_PATH, which is defined as 260 characters. In later versions of Windows, changing a registry key or using the Group Policy tool is required to remove the limit.

In the Windows API (with some exceptions discussed in the following paragraphs), the maximum length for a path is MAX_PATH, which is defined as 260 characters. A local path is structured in the following order: drive letter, colon, backslash, name components separated by backslashes, and a terminating null character. For example, the maximum path on drive D is "D:\some 256-character path string" where "" represents the invisible terminating null character for the current system codepage. (The characters < > are used here for visual clarity and cannot be part of a valid path string.)

Source:

Ramhound
  • 44,080
2

There are two limits to consider: one is the maximum path length, the other the maximum file name length. On NTFS, file names can contain up to 255 UTF-16 code points, with no null byte (see p. 12).

abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefghijklmnopqrstuvwxyz_abcdefgh.txt is exactly 255 characters in length.