Why does the 260 character path length limit exist in Windows?

后端 未结 11 993
孤城傲影
孤城傲影 2020-11-22 03:18

I have come up against this problem a few times at inopportune moments:

  • Trying to work on open source Java projects with deep paths
  • Storing deep Fitne
相关标签:
11条回答
  • 2020-11-22 03:47

    One way to cope with the path limit is to shorten path entries with symbolic links.

    For example:

    1. create a C:\p directory to keep short links to long paths
    2. mklink /J C:\p\foo C:\Some\Crazy\Long\Path\foo
    3. add C:\p\foo to your path instead of the long path
    0 讨论(0)
  • 2020-11-22 03:49

    Quoting this article https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation

    Maximum Path Length Limitation

    In the Windows API (with some exceptions discussed in the following paragraphs), 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. 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.)

    Now we see that it is 1+2+256+1 or [drive][:\][path][null] = 260. One could assume that 256 is a reasonable fixed string length from the DOS days. And going back to the DOS APIs we realize that the system tracked the current path per drive, and we have 26 (32 with symbols) maximum drives (and current directories).

    The INT 0x21 AH=0x47 says “This function returns the path description without the drive letter and the initial backslash.” So we see that the system stores the CWD as a pair (drive, path) and you ask for the path by specifying the drive (1=A, 2=B, …), if you specify a 0 then it assumes the path for the drive returned by INT 0x21 AH=0x15 AL=0x19. So now we know why it is 260 and not 256, because those 4 bytes are not stored in the path string.

    Why a 256 byte path string, because 640K is enough RAM.

    0 讨论(0)
  • 2020-11-22 03:49

    You can enable long path names using PowerShell:

    Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name LongPathsEnabled -Type DWord -Value 1 
    

    Another Version is to use a Group Policy in Computer Configuration/Administrative Templates/System/Filesystem:

    0 讨论(0)
  • 2020-11-22 03:55

    You can mount a folder as a drive. From the command line, if you have a path C:\path\to\long\folder you can map it to drive letter X: using:

    subst x: \path\to\long\folder
    
    0 讨论(0)
  • 2020-11-22 03:59

    As to how to cope with the path size limitation on Windows - using 7zip to pack (and unpack) your path-length sensitive files seems like a viable workaround. I've used it to transport several IDE installations (those Eclipse plugin paths, yikes!) and piles of autogenerated documentation and haven't had a single problem so far.

    Not really sure how it evades the 260 char limit set by Windows (from a technical PoV), but hey, it works!

    More details on their SourceForge page here:

    "NTFS can actually support pathnames up to 32,000 characters in length."

    7-zip also support such long names.

    But it's disabled in SFX code. Some users don't like long paths, since they don't understand how to work with them. That is why I have disabled it in SFX code.

    and release notes:

    9.32 alpha 2013-12-01

    • Improved support for file pathnames longer than 260 characters.

    4.44 beta 2007-01-20

    • 7-Zip now supports file pathnames longer than 260 characters.

    IMPORTANT NOTE: For this to work properly, you'll need to specify the destination path in the 7zip "Extract" dialog directly, rather than dragging & dropping the files into the intended folder. Otherwise the "Temp" folder will be used as an interim cache and you'll bounce into the same 260 char limitation once Windows Explorer starts moving the files to their "final resting place". See the replies to this question for more information.

    0 讨论(0)
提交回复
热议问题