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/[email protected]/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/[email protected]/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!