How to set up smartphones and PCs. Informational portal
  • home
  • Interesting
  • Removing long filenames. Choosing a long path (or goodbye MAX_PATH)

Removing long filenames. Choosing a long path (or goodbye MAX_PATH)

Many Windows PC users, not to mention developers, are familiar with the problems when working with long (more than 260 characters, MAX_PATH) file or directory paths.

This article discusses ways to get rid of this vestige when developing applications on various platforms (WinApi, .Net Framework, .Net Core) and enable native support for long paths in Windows 10 (Anniversary Update).

Win API Applications

In applications that use the Win API to work with files, the recipe for getting rid of the MAX_PATH restriction has been known since time immemorial - it was necessary to use the Unicode version of the function with the ending "W" to work with a directory or file and start the path with the \\?\ prefix. This made it possible to use paths up to 32767 characters long.

In Windows 10 (1607), the behavior of the file functions has changed, with the ability to disable MAX_PATH constraint checking at the system level.

This affected the following features:

To work with directories: CreateDirectoryW, CreateDirectoryExW, GetCurrentDirectoryW, RemoveDirectoryW, SetCurrentDirectoryW. And for working with files: CopyFileW, CopyFile2, CopyFileExW, CreateFileW, CreateFile2, CreateHardLinkW, CreateSymbolicLinkW, DeleteFileW, FindFirstFileW, FindFirstFileExW, FindNextFileW, GetFileAttributesW, GetFileAttributesExW, SetFileAttributesW, GetFullPathNameW, GetLongPathNameW, MoveFileW, MoveFileExW, MoveFileWithProgressW, ReplaceFileW, SearchPathW, FindFirstFileNameW, FindNextFileNameW, FindFirstStreamW, FindNextStreamW, GetCompressedFileSizeW, GetFinalPathNameByHandleW.


This eliminates the need to use the \\?\ prefix and potentially gives applications that work directly or indirectly through the Win API a chance to get support for long paths without having to rebuild them. How to activate this feature is described at the end of the article.

.net framework

Although the .Net Framework uses the Win API to work with files - the previous change would not have worked, because. Preliminary checks for the validity of the names of directories and files are built into the BCL code, and it didn’t even reach the call of the Win API functions, giving a well-known exception. Due to numerous requests from the community (more than 4500 on UserVoice), in version 4.6.2, checks for path length restrictions were cut out of the BCL code, leaving it at the mercy of the operating and file systems!

Here's what it gives:

  • When using the “\\?\” prefix, we can work with long paths like in Win API, Directory.CreateDirectory("\\\\?\\" + long_dir_name);
  • If you enable native support for long filenames in Windows 10 (1607), you don't even need to use a prefix!
How to turn it on:
  • Use .Net Framework 4.6.2 as the target when building the application.
  • Use the configuration file, for example, if the application has already been built under .Net 4.0:

.Net Core

Here support for long paths was announced back in November 2015. Apparently the Open Source nature of the project and the lack of a strict need to ensure backward compatibility affected.

How to turn it on:
Everything works out of the box. Unlike the implementation in the .Net Framework, there is no need to add the “\\?\” prefix here - it is added automatically if necessary.

Here you can see an example.

How to enable long path support in Windows 10 (1607)

This feature is disabled by default. This is because this feature is experimental and there is a need to develop various subsystems and applications to fully support it.

You can enable built-in support for long paths by creating or modifying the following system registry setting: HKLM\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled (Type: REG_DWORD) 1 means enabled.

Or through group policies (Win+R\gpedit.msc) Computer Configuration > Administrative Templates > System > Filesystem > Enable NTFS long paths. It's in the localized version: Computer Configuration > Administrative Templates > System > File System > Enable Win32 Long Paths.

Further, the sources disagree about the manifesto (or I misunderstood, but at the moment I have no way to check). For example, the MSDN documentation says that the manifest can be used as an alternative way to enable long path support in individual applications, and the MSDN blog states that this is the second required step after enabling it in policies.
But they converge in the format of setting this option:

true
With CMD, unfortunately, this will not work, at the moment, due to the peculiarities of working with paths, but in PowerShell everything should work.

P.S.

This concludes my little Friday post, leaving out questions about the completeness of the implementation of support for long paths in Windows 10 (1607), or performance when using various combinations of Windows editions, file systems and APIs. As new facts and experimental results become available, the post will be updated.

Thank you for your attention!

The heading turned out to be long, but it indicates the essence of the problem, which will be described here. I recently ran into a not-so-good situation. While sorting on the hard drive and clearing it of unnecessary, I found that one of the files was not deleted, but the following error appeared: “The file system does not support such long source file names. Try moving the file to a folder with a shorter path length, or provide a shorter file name and try again."

Not only could I not delete the file, but I also failed to change the name to a shorter name. For many, this would seem like a dead end, because it turns out that the file is not deleted.

In fact, the problem is solved very simply and quickly, and now I will describe two methods for solving it, and it’s up to you to decide which one to use.

What to do if the path to the source is too long, how to delete such a file?

If you have a file that is not deleted due to a long name, there are several options to solve the problem. For example, a file is nested in several directories, say, five or even ten folders that have names of 10-20 characters each, then this may be the reason for not deleting the file. The fact is that Windows does not support any files whose names have more than 260 characters. If this limit is exceeded, then the error that I showed above will appear. The number of folders that have their own names and the file name itself must add up to less than 260 characters. The system also has one feature - if this limit is exceeded, we can still rename folders. And this means that you need to rename all folders to a short name, for example, a letter or a number.


Such a problem may appear not only because you gave such a long name. It happened to me when I decided to download the site files from the server and do something. Since the files were no longer needed, I deleted the entire directory with the site files, but as it turned out, it contained a file with a very long name, plus folder names, and this did not allow me to delete it all.

Method two will help if the first one fails. The fact is that not all folders leading to a file with a long name can be renamed and the file system error will still crash. Then you can create a virtual drive using any of the subfolders leading to the non-removable file. Now I will show you how to do it.

Open command line. To do this, press the combination Win + R and enter the command cmd. You can right-click on the Start menu (or the keyboard shortcut Win + X) and select the command line there.

Now you write a command that will make a virtual disk for us:

You need to choose a drive letter that is not occupied by others. The system partition is usually indicated by the letters C, and the second partition, as D or E, then choose any other.

The path to the folder must be specified from the disk. If this causes difficulty for you, then open the folder that will act as a virtual disk and copy the path to it in the explorer at the top.

My command looks like this:

When we go to My Computer, then there you should see a disk with the letter that we assigned to it from the command line. Thus, the path to the file will be reduced by almost half, which means that the total length of characters can be less than 260 characters. Now you can try to get rid of the undeletable.


In order to delete a virtual disk, you need to enter the command:

substDriveLetter /d

Using Total Commander

On the Internet, I found a method that allows you to delete non-removable files using . Of course, the downside here is that you have to install additional software on your computer, and not everyone wants to do this. In any case, I'll leave this method here, in case someone wants to use it.

Download and install the program. Let's go to the tab "File" click on a section "Change Attributes". A window appears where we remove the checkboxes from all attributes. If the checkbox next to the item is not at the top "Process the contents of directories"- choose. Now from the same program we try to delete a file with a long name.


We use the 7-Zip archiver

For this case, we need the 7-Zip archiver. Right-click on the home directory and move the mouse over the partition "7-Zip" and choose the item "Add to archive".

A window appears in which you need to check only one checkbox "Delete files after compression". Then click OK.

An archive of all this goodness is created on the desktop, and folders with a long file are simply deleted. You can delete the archive itself without problems.

Here are the ways to fix the error of a file that cannot be deleted, we have considered. Do you have any other options on how to do this?

Does the harmful Windows operating system prevent you from deleting a file and screams that it has “Source path too long”? In this article I will tell you how easy and simple, without any programs, to defeat this misfortune by shortening the file name.

Too long path to the source - why?

I want to explain right away why such a system warning appears and it is not possible to perform elementary copying or deleting actions with a file (folder) ...

The notification itself contains the answer to this question - the Windows file system does not support (does not understand) source file names longer than 255 characters.

But do you have a short file or folder name, like "Vacation Photo"? The fact is that the "stupid" system perceives the entire path to the file as its name. If your file or folder is somewhere too deep in the file manager, then its name for it will look something like this ...

So the symbols of the name are going into a gang of more than 255 pieces.

How to delete a non-removable file or folder in this case? Very simple - you need to shorten the file name (path) by renaming nested folders.

How to delete a file with a long name

I will show by my own example how to delete a file with a long name, which, as the system writes, has a too long path to the source.

Yesterday I decided clean up your computer from old backups of the site, but they did not want to be deleted. As you guessed, there was a warning about the too long path to the source ...

Starting from the very first folder in this long journey - renamed a few pieces ...

…and tried again to get rid of the file with a now shorter name - it went safely to the land of deleted files.

Naturally, the new file (folder) name must consist of one character or number, as in my example.

This is how the problem of deleting files or folders with a long name is simply solved. This is not the only method, of course. Can delete with special software or through the registry - I just showed the manual way. For someone it will take a minute, and for someone it will take half an hour - it's up to you how to shorten the file name and the path to the source is too long.

To new useful tips and interesting computer programs.

Here is an example of a folder structure, which I called the "big doll":

As you can see, on the “D” (Data) drive there is the original video folder, in it the “Movies” folder, in it “Everything you need to watch movies” and then in the same way a folder in the folder. At the same time, many folders have long names - more than 10-15 characters, which in total will give the volume of characters in all names more than 260. And if at the end of such a "matryoshka" there is a file, it may turn out that you will not be able to work with it you can ... Sometimes the system itself does not allow you to end up creating a folder or file that exceeds the allowed number of characters, but sometimes for some reason this happens.

In the beginning, for beginners, I will explain what the path to the file and folder is. A path is the so-called address on a computer where a file or folder is located. For example, a file called "My file.txt" is located on the local drive D: in the "Documents" folder. Then the path to this file will look like this:

D:\Documents\My file.txt

Or, for example, this is how the path from the example above will look like, where I showed a complex folder structure.

As you can see, the number of characters in the path above is large and it is possible that at the end there will be a file or folder with a long name that you cannot delete.

When you try to delete a file with a long path name, you will get an error window like this:

I tried to artificially recreate the problem and this is what I got.

On disk "D" I created a folder with a short name "1" and placed a file with a very long name in it. Here is an example:

Now I also rename that folder “1” to some very long name, for example:

Interestingly, Windows freely allows you to rename the folder to such a long name, despite the fact that the number of characters in the name of this folder + file name already exceeds 260! Well, after such a renaming of the folder to a long name, I can no longer delete the file located in it and I get the error shown.

Or here's another example ... I maintain the website of the Moscow Instrument-Making College, where I previously worked full-time, and sometimes I use a copy of the site on my computer to test some new "goodies" for the site. And then all of a sudden I install something bad and the site on the hosting dies? :) And so I check on my computer and if everything is in order, then I already set it up on a real site located on the hosting. So, once I transferred the entire site from the hosting to myself in order to test one thing, and after I finished working, I decided to delete the folder with the site from the computer. After all, it weighs about 6 GB. The whole site was deleted, except for a number of folders. I started looking and saw that those folders that contained a file with a long name in the form of hieroglyphs were not deleted:

I tried again to delete this file with a long name and saw the same error that I mentioned above. I tried to rename it, it doesn't work either. Well, I had to think about how to solve the problem.

How to solve the problem of deleting a folder / file with a long path name!

I tried 2 ways how to delete a folder / file with a long name in the path to them. Both of them are not technically difficult (especially the 1st), so I think a beginner can handle it if he does everything exactly as I show:

    Rename several folders in the file path to a shorter name. This is the simplest solution that should already help a lot! Suppose you have a folder with a very long name that contains a file that you cannot delete, open, copy, and generally cannot work with.

    Then just try to rename the folder that contains the file with a long name, for example, to "1". Let's go back to one of the examples above. Here is a folder with a long name:

    I rename the name of that folder to “1” through the standard Windows Explorer and this is how the path to the file looks like now:

    Now, you can safely work with the file, the system will let you open it, rename it and delete it.

    If you have a long folder tree, i.e. like a nested doll, in one folder there is another, in it more, then more, then start renaming from the very first folder from this nesting doll. Not the last on the list, but the first!

    It may be that there are a huge number of folders and renaming some of them will not work due to the same error or it will take you a lot of time. In this case, I suggest the second method.

    The method is that you can take some folder, for example, in the middle of the general tree and connect it as a virtual disk. A virtual disk is something that reminds us of a regular local disk, but which is simply tied to a specific folder, for example, to quickly interact with it.

    Let's say you have something like this long path:

    D:\Video\My Movies\Everything you need to watch movies\Movie software\How to open MP4 movies\List of MP4 sample movies\List of programs to open MP4\What not to do when opening MP4 files

    Suppose that in the very last folder we have some kind of problematic file that we cannot work with, since the path to it, as we see, is very decently long :)

    We can take and connect one of the folders with a long name in the middle of the path as a virtual drive. Let it be the folder "Programs for watching movies".

    To connect a virtual disk, we need the so-called Windows command line, i.e. console.

    You can open it through Windows search. In the search and type "Command line":

    In the command line window, we need to type the command:

    subst virtual_disk_letter "path_to_folder"

    You can put any drive letter, as long as it is not already occupied by one of the local drives. For example, you have a local drive C and D, which means that you can no longer call the virtual drive the same letter. It can be called, for example, the letter "X", because it is rarely used by anyone in Windows.

    As the path to the folder, you insert the path to the folder that you want to connect as a virtual one. As I said, in my example, we will make “Programs for watching movies” a virtual folder, which means that the path must be specified like this:

    D:\Video\My Movies\Everything you need to watch movies\Movie software\

    So, the final command in my case will look like this:

    subst X: "D:\Video\My Movies\Everything You Need to Watch Movies\Movie Watchers\"

    To execute the entered command, press the "Enter" key on the keyboard. The virtual disk will be created.

    Now let's go to Windows Explorer, go to the "This PC" (or "Computer") section and see the created virtual disk there:

    You can distinguish it by letter. I assigned the letter "X" to the virtual drive. Now if you open this virtual disk, we will immediately find ourselves in the "Programs for watching movies" folder. Those. we took and cut off half the path to the final folder.

    For comparison…

    Previously, the path to the destination folder looked like this:

    D:\Video\My Movies\Everything you need to watch movies\Movie software\How to open MP4 movies\List of MP4 sample movies\List of programs to open MP4\What not to do when opening MP4 files

    After connecting the virtual disk to the folder "Programs for watching movies", the path to the final folder looks like this:

    X:\How to open MP4 movies\List of sample MP4 movies\List of programs to open MP4\What not to do when opening MP4 files

    Feel the difference? The path is shortened by half, since the drive letter immediately directs us to the folder in the middle of the path :) And since the path is greatly shortened, now you have probably removed the character limit in the names of files and folders and you can delete a file or folder with a long name!

    After you solve the problem with the file, the virtual disk can be deleted again so as not to be an eyesore in Explorer.

    To do this, open the Windows command prompt again and enter the command:

    Subst virtual_disk_letter /d

    In my case, the drive letter is "X", so the command will look like this:

When I thought about the options for solving the problem, how to delete a folder or file with a long name, I found another option on the Internet: delete the problematic file through the Total Commander program. I tried it, it didn’t work, so I didn’t advise this method :)) And the method, even if it would work for someone, is honestly not cool, because you have to install an additional program on your computer in order to delete only one file or folders with a long name...

I think that if you encounter a similar problem that I have encountered more than once, one of the above methods will definitely help you.

That's all! See you soon in the next articles;)

Top Related Articles