42

One of our customers habitually use very long path names (several nested folders, with long names) and we routinely encounter "user education issues" in order to shorten the path to less than 260 characters.

Is there a technical solution available, can we flick some sort of switch in Windows 7 and Windows 2008 R2 to say "yeah just ignore these historical problems, and make +260 character path name work".

P.S. I have read and been totally unedified by Naming Files, Paths, and Namespaces

Christopher Edwards
  • 688
  • 1
  • 8
  • 19

4 Answers4

33

Just mentioning a trick I do not see mentioned here yet.

Take this file for example:

C:\Folder1\Really Long Path\Such Recursion\So Deep\Wow\Still Going\I will run out of ideas soon\I have organizational problems\Obsessive compulsive subdirectory disorder\Here is a guid for no good reason\936DA01F-9ABD-4d9d-80C7-02AF85C822A8\Almost there\Tax Returns\2013\2013_tax_return.pdf

This full file path is 290 characters long. The shell (Windows Explorer) and most command line utilities probably won't let you touch it.

Use the subst command like so:

subst X: "C:\Folder1\Really Long Path\Such Recursion\So Deep\Wow"

Now you can access (and delete, move, etc.) the file thusly:

X:\Still Going\I will run out of ideas soon\I have organizational problems\Obsessive compulsive subdirectory disorder\Here is a guid for no good reason\936DA01F-9ABD-4d9d-80C7-02AF85C822A8\Almost there\Tax Returns\2013\2013_tax_return.pdf

And now that file name is only ~235 characters or so, so you will not encounter the "Filename is too long" problems any more.

In the Windows API, there is an infamous constant known as MAX_PATH. MAX_PATH is 260 characters. The NTFS file system actually supports file paths of up to 32,767 characters. And you can still use 32,767 character long path names by accessing the Unicode (or "wide") versions of the Windows API functions, and also by prefixing the path with \\?\.

MAX_PATH was set in stone a very long time ago in the Windows world. I think it has something to do with ANSI standards at the time... but it's one of those things that's very difficult for Microsoft to change now, as now we have thousands of programs and applications, including some written by Microsoft themselves, that use MAX_PATH and would fail in strange new ways if the constant were suddenly changed. (Buffer overflows, heap corruption, etc.)

Ryan Ries
  • 55,011
  • 9
  • 138
  • 197
  • 10
    I've been taking an approach similar to `subst` by using directory junctions, which may be more convenient to cleanup if you're just looking to make a few quick changes. I use [Link Shell Extension](http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html), but you can just use the `mklink` command like so: `mklink /J C:\Wow "C:\Folder1\Really Long Path\Such Recursion\So Deep\Wow"`. – Charles Grunwald Sep 27 '15 at 03:10
13

The methods are there, but until Microsoft re-codes the file-browser widget we're pretty much stuck with that old problem. It's sub-optimal, but that's just how it works.

sysadmin1138
  • 131,083
  • 18
  • 173
  • 296
  • 7
    Bug in MS software that has been unfixed for few OS releases, that's a new one... – Hubert Kario Nov 24 '11 at 00:01
  • 3
    Windows 10 1607 finally fixed the file-browser problem and has some wizardry to fool Win32 applications in to working in this strange new world of huge paths: https://www.howtogeek.com/266621/how-to-make-windows-10-accept-file-paths-over-260-characters/ – HackSlash Jul 31 '18 at 22:44
13

You can get around this limitation by using the \\?\C: notation. It's ugly, but it supports file lengths up to 2^15.

http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx#maxpath

jscott
  • 24,204
  • 8
  • 77
  • 99
Chris K
  • 659
  • 4
  • 11
  • 2
    Yeah I read that, as I said (see my link). But how do I benefit from it exactly? Can I map a drive to \\?\something for example. I don't understand. The article is intended for programmers using windows APIs. Whilst I do code in .NET as the need arises I don't use C++ etc. and can't see how this is relevant for my customers staff who are using windows explorer and the common file dialog etc. – Christopher Edwards Feb 09 '11 at 00:08
  • 1
    Ultimately, there is none. :-/ Sadly. – Chris K Feb 09 '11 at 03:35
  • @ChristopherEdwards You can create junctions to points lower in folder hierarchy. – Hubert Kario Nov 23 '11 at 23:59
  • @HubertKario Yes, but how would that help me? – Christopher Edwards Nov 28 '11 at 23:58
  • 1
    @ChristopherEdwards you could do a link `c:\usr-data` to `c:\Users\VeryLongUserName\LongFolderName\` and tell him to use the former, but if he regularly names folders in 40-something characters then this won't help... – Hubert Kario Nov 29 '11 at 20:50
  • 2
    7-zip will happily browse and copy in \\?\C: notation, for what it's worth. – Matt Lyons-Wood Nov 24 '15 at 06:17
  • This also works in standard command line commands like DIR and COPY. – David Balažic Apr 07 '19 at 13:24
9

Microsoft now has an available fix for this beginning with Windows 10, which is explained in the Naming Files, Paths, and Namespaces MSDN article.

darthcoder already answered with details on the \\?\C: notation work-around, but there is now a registry key at HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD) which can be used to remove MAX_PATH limitations for a single system. The article also mentions support for Group Policy control at Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths for organization or group-wide implementation if needed.

JimNim
  • 2,736
  • 12
  • 23
  • I've not had time for thorough testing, but I've seen mixed results with this solution so far, and can't confirm that this solves the problem for Windows [file] Explorer. – JimNim Apr 19 '17 at 13:57
  • 2
    This doesn't work in File Explorer because although the system and NTFS can handle long paths, Explorer cannot until it is itself re-coded. – ingyhere Dec 25 '17 at 18:50
  • PS, you will need to use one of the copy programs in [this SO link](https://superuser.com/questions/262981/do-fast-copy-programs-work) on top of Explorer to actually make it work. – ingyhere Dec 25 '17 at 18:51
  • 1
    Good info! I will probably just stick to Robocopy in that case. – JimNim Dec 26 '17 at 05:59
  • where is darthcoder's posting? did he delete it? – barlop Apr 29 '20 at 11:21
  • Apparently it's been deleted... strange. There's some good info on it here though: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file - essentially, the notation allows you to reference file paths that are more than 255 characters long. Works for file share paths too, via \\?\UNC\server\share\. – JimNim Apr 30 '20 at 15:24