HOWTO: Easily delete folder paths with long names – for free!

If you’ve been in IT for any length of time, you’ve run into issues where you’ve been asked to delete folders that users have created that are longer than 260 characters.  If you try to delete the folder, you get something like:

image

So you start Googling and invariably you’ll find the same tool recommended over and over again – the Long Path Tool from www.longpathtool.com.  I have to give the guy that wrote it credit.  He has completely saturated the search engines with his solution for a problem that by all accounts shouldn’t even exist.  At any rate, you grab his tool and try to run it only to be greeted with:

image

I don’t know about you, but I all but refuse to buy a tool for something that should be a solved problem in 2014.  I knew that robocopy was a built-in tool for every windows installation and I knew that it had support for paths up to 32,000 characters long.  I thought I could use that.  Unfortunately robocopy doesn’t offer any native support for delete data, only copying it as per its namesake.  But with robocopy being the only native tool I was aware of that could solve the problem of deleting folders with long file names, I decided to spend some time with it.

If you think outside the box a bit, you quickly realize that robocopy can delete folders if you approach the problem from a different angle.  Robocopy has a “mirroring” function that will mirror a source folder to a destination folder.  If the source folder is empty, the contents of the destination folder are deleted.  Ah ha!

I decided to try and wrap this idea around in an easy to use PowerShell cmdlet.  I ended up falling way, way down the rabbit hole on this one as I strived to create the most comprehensive and complete cmdlet I’ve ever built.  The objective was to make it robust enough that it could be mistaken for an “official” cmdlet.   I’m pleased to report that I have been largely successful.  If you have an need to delete a bunch of folders that contain long file names, read on to find out how to do so for free!

First you’ll need to download the PowerShell module I created.  You can do that here:

image

https://pleasework.robbievance.net/PowerShellScripts/RobbieVance.PowerShell.FolderRemoval.zip
Note: This script requries that the host where it executes has PowerShell 3.0 or newer installed

Place the file somewhere on your system, lets say c:\scripts.  Open an administrator PowerShell prompt (required, the script will prompt you if you did not) and type Import-Module c:\scripts\Robbie.Vance.PowerShell.FolderRemoval.psm1
The command you have have available to you is called Remove-FolderUsingRoboCopy
You can get full help of the command by typing Get-Help Remove-FolderUsingRoboCopy –Full which includes a bunch of examples.  Let’s just stick with a simple one.  The Long Path Tool includes a button called Press here to view demonstration that creates a folder/file path longer than 1000 characters.  If you press it you’ll get this:

image

Windows won’t work as it generates the error I shows you above.  But we sure as hell aren’t buying a tool for this.  No instead simply type:

Remove-FolderUsingRobocopy c:\abc [tab] (to autocomplete as you don’t want to type all of that) -whatif

You will be prompted to confirm the delete operation.  However, since we are in whatif mode, no changes will be mode.  Press Y. The file will delete successfully.

image

If you’re happy with the results, simply remove the –whatif and run it again.  If you’d like to not be prompted, use the –force parameter.

That’s all there is to it!

There are some other features if you need them though.  Notably the command line fully supports pipeline input so if you for example have a list of folders in a CSV, you can simply use Import-CSV and pipe them into the command. There is also a full logging and statistics facility as well as graphical progress bars for longer running delete operations. This script will take in to account UNC network based shares as well as gracefully log any permission denied errors.  I suspect that for many people, this script should eliminate the need to purchase the Long Path Tool.

I hope you find this useful.

– Robbie

3 comments

    • FGranados on January 26, 2015 at 6:37 am
    • Reply

    Long path tool is the best solution for your problem. try it and solve your problem

    • Ronald on August 22, 2016 at 12:53 pm
    • Reply

    yep Long Path Tool worked for me. thanks

    • Brandon Wooten on August 8, 2018 at 7:31 am
    • Reply

    you should try long path tool…it worked for me.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.