#StandWithUkraine

Delete too long paths (folder trees) with robocopy

Earlier this weak I was cleaning up virtual server for another round of torturous (to me) software experiments. I’ve noticed that one of heavy applications claimed to uninstall correctly, but left folders behind. Folders I was unable to delete.

After few attempts and tools like Unlocker failed (sadly I had no access to Total Commander at moment) I tried to simply browse those folders and umpteenth levels deeper I got message that path is too long to navigate.

Theory

Microsoft offers extensive knowledge base article “You cannot delete a file or a folder on an NTFS file system volume”. In short it tells you that very long paths are pain to kill so you better look for software that can do that or enjoy intricate operation of remaining folders until path isn’t too long.

I’d prefer to read that they taught Explorer to do perform its file managing duties with at least some kind of effort to not fail miserably.

Practice

Luckily several Google search result lower there was an article with command line script for exactly this issue. Copy/pasting it here won’t be polite (or copyright-friendly) so grab it from there.

What it does

Since article offered no explanation, except mention that it relies on robocopy I decided to look into it. Turns out it uses robocopy in rather clever way.

  1. Takes folder to delete as command line parameter.
  2. Creates empty folder in system temporary folder.
  3. Runs robocopy, instructing it to copy empty folder over target and destroy everything that is absent in former ( /MIR key).

Essentially robocopy is instructed to overwrite target folder tree and files in it with empty directory. And, as I tested, it does an excellent job – without any issues with long path.

As usual I must warn that such kind of script can be extremely destructive, use cautiously.

Overall

It would have never occurred to me to use robocopy in such way. It shows just how powerful modern command line utilities can be, despite seemingly archaic interface paradigm.

Related Posts

1 Comments

  • Kellymin #

    people often manage to create those files with so many characters in the past in the first place. They PUT the files there using a mapped drive letter, and therefore the path was short enough. Later, when someone tries to browse there the long way, using the full path, they have problems. I don't understand why Microsoft hasn't changed this, it's been a problem for a decade or more!