Windows: how to delete repeating “infinite” nested folder structure

This can happen with a lot of different programs, or even with messing up a script or something: some loop happened at some point when copying/creating files on a disk, and you get a repeated folder structure clogging your storage.

I’ve had the issue with Robocopy, a vintage (1996!) .exe perfect for scripted simple file backups. Hey, the latest release is from 2019 after all 🙂

I used it to copy some user’s AppData in Windows 10, to another disk. The process took ages, which seemed weird to begin with, as the user’s AppData was about a few GB’s.

The command with parameters I used was the following:

The command ran for more than an hour, and then seemed to be stopped. Yeah, the mentioned F:\ disk was 100% full.

The result was pretty amazing. I solved the issue since I’ve had it so the screenshot below is made-up, but this is kinda faithful to the situation I had on the backup drive ( F: )

So the folder “Username” was containing its usual “Local”, “LocalLow” an “Roaming” directories, plus their orresponding files. But inside the “Local” directory, you have the whole AppData with its three sub-folders and all files again, recursively, and bearing the full name “Application Data”.
I think it went to about ~20 levels of depth, until the disk was full. I don’t understand how this happened, probably some bug with Robocopy unable to process the users directories correctly in Windows 10.

I wanted to delete the root folder “Username” to clean all that crap, and it was plainly impossible: Windows was getting lost in the depth, with long files, and ACLs (maybe that /copyall option on Robocopy wasn’t such a good idea to begin with…)

I had to use two tricks:

First trick is to take ownership of the whole content of the folder, as far as the command could go:

To be executed in elevated cmd, and ensure your user is among the administrators of course. If needed, check the parameters of takeown.exe and adapt to your needs with your specific user.

Second one is courtesy of Blackbam’s (1), and is a pretty ingenious little batch file (in elevated cmd/PS as well):

So you rename your first looping sub-directory to “x”, you move the content of x’s subdir to the root (including the looping directory within), and delete x as well as its content. If you have a humongous depth level, you can even loop that into some PowerShell or batch if you remember that 😉

I had a little issue with rd unable to delete some long filename within a “Microsoft” directory, but once takeown had done its job and the folder moved to a higher level, the Windows Explorer was enough to perform the deletion… some tedious manual job but I hadn’t that many depth levels in the end..

And that’s it, I managed to free all the space eaten by this annoying situation.

Morality: don’t use Robocopy to backup user’s AppData! 🙂

Sources:

What is locking my file or folder?

We’ve all had this.  You want to delete or rename some file or folder, and you get the very dreaded following window:
filelock-cannotdelete

You probably already know about the locked files and checked the Server Manager > Roles > File Services > Share and Storage Management > OpenFiles   (this is the path for Win 2K8).  Your folder is not to be seen, and it gets you mad, I know.

Then you Google some, and you find some miraculous third-party software which-will-solve-all-your-problem-but-please-click-next-next-next-and-don’t-read.

filelock-unlocker
Delta Toolbar?  SRSLY?

Then let’s think: it’s Windows, we are in 2015; what about Powershell?

Beam Us Up Scotty proposes a command for this, let’s try it on that example directory:

It might be actually good for files, but in the case of a directory, it returns nothing 🙁

Out of the box, I haven’t found a way to get what I want:  clearly identify what the hell is locking my file or folder.

There is a Sysinternals executable called Handle.exe (which I would love to decompile some day and inject its commands into some PS!) which looks promising.

Note that you can run this executable from a remote location, but not from a certain location, aimed toward a remote machine where the file is locked.

The Lonely Administrator and Stackoverflow propose solutions using this, let’s try:

  1. Download Handle.exe from the official Sysinternals site. It’s a safe lonely executable, no crappy toolbar, no adware and the like.
  2. Put it on some network share.
  3. Write a nice PS function to use Handle.exe. As we won’t reinvent the wheel; just grab the one from the Lonely Administrator (the second “Click to Expand Code”)… it’s well done and works fine (*).
  4. Put the function in a file (let’s call it Get-LockingProcess.ps1) on a share (e.g. the same as Handle.exe, let’s call it MYSHARE for the example).  Be sure to edit the path to the Handle.exe file (variable $Handle in that code) to match your network share.
  5. From anywhere where a file or folder is locked, as long as you have access to the said share, you open a Powershell window, you include the Get-LockingProcess.ps1 (using the dot command for instance) and then you call the function with the locked directory.

Let’s try this with our example dir.  I put Handle.exe and the Get-LockingProcess.ps1 files on \\MYSHARE:

Pretty clear: you have the directory opened in a cmd window!

This is from a system admin’s point of view and wanting to give any power user a quick and easy way to identify locked files/folders.
Of course you can also do this locally, and/or only output Handle.exe to a file and Ctrl+F into that file, but it isn’t as fancy ! 🙂

 

(*) For future’s sake, here is a copy, with cited source:

Doesn’t that regex totally PWNS? 🙂