7

I have several level of directories in the folder path. when the path exceeds 256, then I could not create a sub-folder or file from it. Is there any chance to build paths more than this length. Can anyone help me out.

Muthukumar Palaniappan
  • 1,622
  • 5
  • 25
  • 49

4 Answers4

7

In fact the limit on path strings is 260 characters. The underlying OS, these days, can support much longer path names, up to 32,767 characters. In order to name a path with a long name you need to use the magic \\?\ prefix, and use the Unicode version of the API.

However, many tools don't support such long names. A classic example of such a tool is Explorer which won't let you create objects with names longer than 260 characters. Because of this I strongly advise you to avoid creating such long names—doing so will save you much heartache in the long run.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    I wonder if there's a "patched", so to speak, version of Explorer that supports long paths by default. – JAB Aug 09 '11 at 13:28
  • @JAB My understanding is that Explorer is this way as policy rather than technical reasons. It's trying to stop users getting themselves into situations where other tools fail. Lowest common denominator. – David Heffernan Aug 09 '11 at 13:30
  • I'm aware of that. Perhaps "hacked" would have been a better word to use than "patched". – JAB Aug 09 '11 at 13:38
  • Dave, Could you please let me know any NTlevel Unicode APis which operates on this long path. – Muthukumar Palaniappan Aug 09 '11 at 13:57
  • 2
    NT paths are all of this form, I believe. The 260 limit is just at the Win32 level. Call `CreateFileW`, prefix your path with `\\?\` and you are good to go. – David Heffernan Aug 09 '11 at 13:59
  • You are right dave. NTlevel apis doing that. many thnaks. am also trying in createfilew – Muthukumar Palaniappan Aug 09 '11 at 14:41
  • "I wonder if theres a patched version of Explorer" - Yes, there is: Total Commander! www.ghisler.com – Gabriel Jan 08 '20 at 08:16
2

This should get you started: http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#maxpath

Sadly it's an issue that I don't think will be going away any time soon, so you'd do well to familiarize yourself with that stuff.

As an aside, if you have access to robocopy (comes packaged with Windows Vista and 7, but is also available for XP), which supports long paths, you could create your files/subfolders in a higher-up folder and then use robocopy to move the subfolder to its desired location deeper in the folder tree.

JAB
  • 20,783
  • 6
  • 71
  • 80
  • 1
    Starting in Windows 10.1607, MAX_PATH limitations have been removed from common Win32 file and directory functions. However, you must opt-in to the new behavior. – Gabriel Jan 08 '20 at 08:04
  • Total Commander ( www.ghisler.com ) supports long paths and it is much better/versatile than robocopy. – Gabriel Jan 08 '20 at 08:18
1

According to the documentation here http://msdn.microsoft.com/en-us/library/Aa365247, the maximum length is actually about 32,000, but most windows APIs still limit you to MAX_PATH which is 260. There are some unicode APIs that let you go beyond the 260 limit.

See here, http://msdn.microsoft.com/en-us/library/aa363856.

In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend \\?\ to the path. For more information, see Naming a File.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Kratz
  • 4,280
  • 3
  • 32
  • 55
0

This is an addendum to the answers above. I extracted only a summary to what I think is relevant, from Microsoft's official documentation:

Maximum Path Length Limitation
In the Windows API (with some exceptions), 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.
Example: "D:\some 256-character-path-string" -> 256

Using long paths
The Windows API has many functions that also have Unicode versions to permit an extended-length path for a maximum total path length of 32,767 characters.
To specify an extended-length path, use the "\?" prefix. For example, "\?\D:\very long path".

Relative paths
Relative paths are always limited to a total of MAX_PATH characters.

Enable Long Paths in Win10
Starting in Windows 10.1607, MAX_PATH limitations have been removed from common Win32 file and directory functions.
However, you must opt-in to the new behavior.

From Microsoft documentation:
https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#maximum-path-length-limitation


Warning for Delphi users:
There is a problem in IOUtils. It cannot be used in conjunction with Max_Path. It uses InternalCheckDirPathParam all over the place!

Details: TDirectory.GetDirectoryRoot does not handle correctly paths of Max_Path characters

Gabriel
  • 20,797
  • 27
  • 159
  • 293