MAX_PATH Topic is solved

Share your ideas as to how the documentation can be improved.
iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

MAX_PATH

Post by iPhilip » 26 Jan 2021, 19:14

In the Long Paths section of the documentation, it states:
These limitations are often referred to as "MAX_PATH limitations", after the constant MAX_PATH, which has the value 260. This allows for the drive letter, colon and slash (C:\), a single file or directory name at maximum length (255 characters), and a null terminator.
Based on the results below, that statement should be restated in one of the following two ways:
These limitations are often referred to as "MAX_PATH limitations", after the constant MAX_PATH, which has the value 260. This allows for the drive letter, colon and slash (C:\), a single file or directory name at maximum length (255 characters), and a null terminator.
or
These limitations are often referred to as "MAX_PATH limitations", after the constant MAX_PATH, which has the value 260. This allows for the drive letter, colon and slash (C:\), a 256-character path string, and a null terminator.
I prefer the second option as it's consistent with Microsoft's Maximum Path Length Limitation page:
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<NUL>" where "<NUL>" 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.)
- iPhilip

Original Post
Last edited by iPhilip on 04 Feb 2021, 17:03, edited 1 time in total.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: MAX_PATH

Post by TheDewd » 27 Jan 2021, 15:51

I think you've missed something...

AutoHotkey wrote:...a single file or directory name at maximum length (255 characters), and a null terminator.
Notice that the AutoHotkey quote says the directory name is 255 characters, AND a null terminator. This would bring the maximum to 256.

Microsoft wrote:...maximum path on drive D is "D:\some 256-character path string<NUL>" where "<NUL>" 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.)
Microsoft is including the null terminator as part of their character limit explanation, while AutoHotkey is separating it. AutoHotkey isn't stating that MAX_PATH is 255 -- only that the directory name character limit is 255. After adding the null terminator, it becomes 256.

AutoHotkey wrote:These limitations are often referred to as "MAX_PATH limitations", after the constant MAX_PATH, which has the value 260.
You can see in the earlier part of the AutoHotkey quote, they indicate that MAX_PATH is 260.

I think the AutoHotkey quote is valid, however I understand how the phrasing of words can easily cause this confusion to occur.

Do you agree?

:?:

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: MAX_PATH

Post by iPhilip » 27 Jan 2021, 22:11

Hi @TheDewd,

Thank you for your thoughts. First, I have a few comments about your rationale.
TheDewd wrote: Notice that the AutoHotkey quote says the directory name is 255 characters, AND a null terminator. This would bring the maximum to 256.
If that were the case then the maximum file path, including the null terminator, would be 256 + 3 = 259, not 260.
TheDewd wrote: Microsoft is including the null terminator as part of their character limit explanation, while AutoHotkey is separating it.
I agree. Both are saying that MAX_PATH = 260. AutoHotkey is saying that the maximum number of characters, not including the null terminating character, is 259. Again, 259 - 3 = 256, not 255.

Next, to demonstrate my point, I would encourage you to run the following script:

Code: Select all

MAX_PATH := 260
MaxChars := MAX_PATH - 1
LongPath := A_ScriptDir "\"
Loop, % MaxChars - StrLen(LongPath) - 4
   LongPath .= "x"
LongPath .= ".txt"
MsgBox % "The file path has " StrLen(LongPath) " characters."
FileAppend, , % LongPath
if FileExist(LongPath) {
   MsgBox The file exists!`n`nClick OK to delete it.
   FileDelete, % LongPath
} else
   MsgBox The file does not exist!
ExitApp
You will see that a file with a path length of 259 can be created but one with 260 characters can't (try it!). If the maximum length of a file or directory name were 255 characters, then the maximum path length would be 255 + 3 = 258 and a path length of 259 would not be possible.

Finally,
TheDewd wrote: Do you agree?
No.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: MAX_PATH

Post by TheDewd » 28 Jan 2021, 09:14

I think I understand the issue now.

iPhilip wrote:If that were the case then the maximum file path, including the null terminator, would be 256 + 3 = 259, not 260.
You're only adding 3 to the filename + null terminator character length.

What I failed to include in my rationale is a trailing backslash, which will bring the total to 260. Note that a trailing slash is part of the PATH, but not the FILENAME.
https://superuser.com/a/1347053

I've found a few different sources that mention adding the final backslash for rounding to meet the 260 limit.

drive letter + :\ + 255 characters of filename + \ + null terminator
1 + 2 + 255 + 1 + 1 = 260

Instead of changing the FILENAME character length, the documentation should include the mention of the trailing slash before the null terminator.

FYI - I'm genuinely curious about this topic. Do you think the trailing slash might be the missing character from the AutoHotkey documentation that has caused this mess? Interested to hear what you think.

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: MAX_PATH

Post by iPhilip » 04 Feb 2021, 16:59

Hi @TheDewd,

Thank you for your interest in this topic.
TheDewd wrote:
28 Jan 2021, 09:14
What I failed to include in my rationale is a trailing backslash, which will bring the total to 260. Note that a trailing slash is part of the PATH, but not the FILENAME.
Nowhere in Microsoft's documentation does it state that a trailing slash is required at the end of the PATH. As I mentioned in my original post, Microsoft states on the Maximum Path Length Limitation page that:
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<NUL>" where "<NUL>" 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.)
There is no mention of a trailing backslash at the end of a file path.
TheDewd wrote:
28 Jan 2021, 09:14
I've found a few different sources that mention adding the final backslash for rounding to meet the 260 limit.
Rounding of what? There is no such thing as fractional characters. I would encourage you to think critically about what other people post (including me) and check with the documentation that Microsoft provides (which, unfortunately, is not always accurate).

I did some tests with the script below and I can make the following observations:

  1. The maximum number of characters in a path is 259 (MAX_PATH - 1).
  2. The maximum number of characters in a file name is 255, e.g. drive letter + :\ + 255 file name characters.
  3. The maximum number of characters in a folder name is 244, e.g. drive letter + :\ + 244 folder name characters.
  4. When combined with a backlash and a file name the maximum number of characters in a folder name is 243, e.g. drive letter + :\ + 243 folder name characters + \ + 12 file name characters. The 12 characters represent the number of characters in an 8.3 file name. This is consistent with the following statement from the Maximum Path Length Limitation page:
    When using an API to create a directory, the specified path cannot be so long that you cannot append an 8.3 file name (that is, the directory name cannot exceed MAX_PATH minus 12).
Here is the script:

Code: Select all

#NoEnv

; Admin priviledges are required to create files and/or folders in the root directory

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
   try
   {
      if A_IsCompiled
         Run *RunAs "%A_ScriptFullPath%" /restart
      else
         Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
   }
   ExitApp
}

MAX_PATH := 260

; Test of longest path in the script directory

Loop {
   MaxChars := MAX_PATH - A_Index
   LongPath := A_ScriptDir "\"
   NoChars := StrLen(LongPath)
   Loop, % MaxChars - NoChars - 4
      LongPath .= "x"
   LongPath .= ".txt"
   FileAppend, , % LongPath
   if FileExist(LongPath)
      break
}
MsgBox % "The file path has " StrLen(LongPath) " characters.`nThe file name has " StrLen(LongPath) - NoChars " characters.`nThe first 25 characters are: " SubStr(LongPath, 1, 25) "`nThe last 25 characters are: " SubStr(LongPath, -24) "`n`nClick OK to delete it."
FileDelete, % LongPath

; Test of the longest filename in the C:\ folder

Loop {
   MaxChars := MAX_PATH - A_Index
   LongPath := "C:\"
   NoChars := StrLen(LongPath)
   Loop, % MaxChars - NoChars - 4
      LongPath .= "x"
   LongPath .= ".txt"
   FileAppend, , % LongPath
   if FileExist(LongPath)
      break
}
MsgBox % "The file path has " StrLen(LongPath) " characters.`nThe file name has " StrLen(LongPath) - NoChars " characters.`nThe first 25 characters are: " SubStr(LongPath, 1, 25) "`nThe last 25 characters are: " SubStr(LongPath, -24) "`n`nClick OK to delete it."
FileDelete, % LongPath

; Test of the longest folder in the C:\ folder

Loop {
   MaxChars := MAX_PATH - A_Index
   LongPath := "C:\"
   NoChars := StrLen(LongPath)
   Loop, % MaxChars - NoChars
      LongPath .= "x"
   FileCreateDir, % LongPath
   if FileExist(LongPath)
      break
}
MsgBox % "The folder path has " StrLen(LongPath) " characters.`nThe folder name has " StrLen(LongPath) - NoChars " characters.`nThe first 25 characters are: " SubStr(LongPath, 1, 25) "`nThe last 25 characters are: " SubStr(LongPath, -24) "`n`nClick OK to delete it."
FileRemoveDir, % LongPath

; Test of the longest folder + 8.3 file name in the C:\ folder

Loop {
   MaxChars := MAX_PATH - A_Index
   LongPath := "C:\"
   NoChars := StrLen(LongPath)
   Loop, % MaxChars - NoChars - 13  ; StrLen("\") + 8 + 1 + 3 (8.3 file) = 13
      LongPath .= "x"
   LongPath .= "\"
   FileCreateDir, % LongPath
   Folder := LongPath
   LongPath .= "12345678.123"
   FileAppend, , % LongPath
   if FileExist(LongPath)
      break
}
MsgBox % "The folder path has " StrLen(LongPath) " characters.`nThe folder name has " StrLen(Folder) - NoChars - 1 " characters.`nThe first 25 characters are: " SubStr(LongPath, 1, 25) "`nThe last 25 characters are: " SubStr(LongPath, -24) "`n`nClick OK to delete it."
FileDelete, % LongPath
FileRemoveDir, % Folder

ExitApp
Thus, the statement in the Long Paths section of the documentation is partially correct in that a single file name, but not a folder name, has a maximum length of 255 characters. I modified my original post to reflect the above observations.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: MAX_PATH  Topic is solved

Post by Ragnar » 07 Feb 2021, 10:27

Thanks for the detailed analysis. The change is included in PR #464.

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: MAX_PATH

Post by iPhilip » 07 Feb 2021, 15:04

Hi @Ragnar, You are welcome. Happy to contribute.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: MAX_PATH

Post by iPhilip » 08 Feb 2021, 14:57

Hi @Ragnar, you might also want to look at the v2 version of the Long Paths section of the documentation. The language there should be corrected as well.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

User avatar
Ragnar
Posts: 613
Joined: 30 Sep 2013, 15:25

Re: MAX_PATH

Post by Ragnar » 08 Feb 2021, 15:38

The changes made in v1 will sooner or later be merged into v2.

iPhilip
Posts: 815
Joined: 02 Oct 2013, 12:21

Re: MAX_PATH

Post by iPhilip » 08 Feb 2021, 15:42

Ok. Thank you.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

Post Reply

Return to “Suggestions on Documentation Improvements”