Tuesday, 23 February 2016

When deleting files - Source Path Too Long

In Windows, when deleting folders/files have you seen this error message:

Source Path Too Long
The source file name(s) are larger than is supported by the file system. Try moving to a location which has a shorter path name, or try renaming to shorter name(s) before attempting this operation.


Windows 8.1 screen shot - it varies depending on your version of Windows.


If copying not deleting?
If you received a similar message when copying files, please see a previous article I wrote on how you may use RoboCopy to help in that scenario:
http://mgxp.blogspot.ch/2015/10/when-copying-files-source-path-too-long.html


Why?
It's due to a limitation of 256 characters of the folder and file name length combined.


Solution
To delete a folder that contains many sub-folders and files with very long file names, there are a few solutions out there including some third-part tools. You could also use the SUBST command to shorten the path but that may not be successful depending on the length of the files/folders you have. However, I've discovered that it's possible to use RoboCopy, the free command line tool that comes with Windows to do the job perfectly!

RoboCopy is for copying files or synchronising folders. I've written a few articles on this tool already - click here for more information. RoboCopy can understand and work with file paths longer than 256 characters. RoboCopy allows you to synchronise folder 'A' with folder 'B'. Imagine that folder 'A' is empty, there are no files in it - if we synchronise that folder with a folder that does have files (folder 'B') then that folder will also be empty. That's the solution. The following is an example of how this works in practice.


Example
I have a folder called 'Folder with very long names'. Inside this folder I have many nested sub-folders with long names. Inside those sub-folders I have many files with very long names. When I try to delete the folder (and sub-folders/files) I get the 'Source Path Too Long' error. 

At the same level of the folder I want to delete 'Folder with very long names', create a new folder called 'Empty':


The folder called 'Empty' will have nothing in it (it should be empty!). 

Open a command window (press Win+R and type CMD [Enter])

At the command prompt change to the same drive/folder where you have the folders 'Empty' and 'Folder with very long names'. 

At the command prompt type the following command:
ROBOCOPY Empty "Folder with very long names" /MIR  [Enter]

The contents of the folder 'Empty' will be mirrored (/MIR parameter) to the folder 'Folder with very long names'. Because the folder 'Empty' has nothing in it, the contents of the destination folder 'Folder with very long names' will end up with nothing in it! Perfect! Now you can remove (delete) the folder yourself without any trouble.


Conclusion
It's very annoying that Windows doesn't properly support folder and file names longer than 256 characters. It's as if they added the functionality and forgot to update all the tools. Even if you try the command line DEL command you can't delete a folder with very long folder/file names. Using RoboCopy is hassle free, it works. Again it has come to the rescue because it can also solve this issue for copying files


Post a Comment