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:
- The maximum number of characters in a path is 259 (MAX_PATH - 1).
- The maximum number of characters in a file name is 255, e.g. drive letter + :\ + 255 file name characters.
- The maximum number of characters in a folder name is 244, e.g. drive letter + :\ + 244 folder name characters.
- 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.