How to create directories in windows with path length greater than 256

13,202

Solution 1

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.

Solution 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.

Solution 3

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.

Share:
13,202
Muthukumar Palaniappan
Author by

Muthukumar Palaniappan

Updated on July 07, 2022

Comments

  • Muthukumar Palaniappan
    Muthukumar Palaniappan almost 2 years

    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.

  • JAB
    JAB over 12 years
    I wonder if there's a "patched", so to speak, version of Explorer that supports long paths by default.
  • David Heffernan
    David Heffernan over 12 years
    @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.
  • JAB
    JAB over 12 years
    I'm aware of that. Perhaps "hacked" would have been a better word to use than "patched".
  • Muthukumar Palaniappan
    Muthukumar Palaniappan over 12 years
    Dave, Could you please let me know any NTlevel Unicode APis which operates on this long path.
  • David Heffernan
    David Heffernan over 12 years
    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.
  • Muthukumar Palaniappan
    Muthukumar Palaniappan over 12 years
    You are right dave. NTlevel apis doing that. many thnaks. am also trying in createfilew
  • Server Overflow
    Server Overflow over 4 years
    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.
  • Server Overflow
    Server Overflow over 4 years
    "I wonder if theres a patched version of Explorer" - Yes, there is: Total Commander! www.ghisler.com
  • Server Overflow
    Server Overflow over 4 years
    Total Commander ( www.ghisler.com ) supports long paths and it is much better/versatile than robocopy.