Wednesday, 12 June 2013

"Path too long" when trying to delete folder - Robocopy to the rescue

One of the many long running mysteries of Windows, is why it's possible to create deep nested directories/files (>256 characters), but not be able to delete/navigate them.

Deep nested (long) directory path, most often manifest around the use of shared folders or the subst command and it can be a hassle to deal with.

There's a few tools around that are designed to target this exact problem, but hiding on everyone system* is a great little program called Robocopy that is primarily designed for copying files. However it can be used to delete them as well.

Here's how you can delete those path too long directories easily, using a single robocopy command.
  • Create an Empty directory
  • Open a command prompt (cmd.exe) - Elevate if necessary.
    • Open the Start menu and type "cmd".
    • Right click the shortcut and select "Run as administrator" 
  • At the command prompt type "robocopy /PURGE <path-to-empty-directory>  <path-to-long-directory>.
    Double check the paths, else risk deleting the wrong files/directory
  • Hit Enter to start the process.
* Robocopy comes standard with Vista and above operating systems. If you are running XP or older, then you can obtain Robocopy as a part of the  Windows Server 2003 Resource Kit Tools