14

On one of our Windows 2003 file servers somehow someone or something managed to create infinitely recurring directory trees within some of the users' home folders, and they can't be deleted.

You wind up with something like this:

D:\Student\2012\user.name\servername\student\2012\user.name\servername\student\2012\...

And it continues infinitely as far as I've been able to tell.

Trying to delete the directory tree from the system it resides on results in "Error deleting file or folder: Cannot delete file: ". Trying to delete it from a Windows 7 system connected to the network share results in a "folder in use" error.

If you navigate down the directory tree as far as you go you eventually reach a directory that cannot be opened or deleted (if right clicked none of the standard options appear), however if you try to open the same directory from a share on another system it can be opened, however Windows Explorer becomes extremely slow.

I've tried the suggestions in this post, as well as booting the server with a Linux disc and trying to rm -rf the directory, neither worked.

It is a mystery.

Edit: I might also add that it's possible to rename the directories, just not remove them.

1

13 Answers 13

26

I also had an endlessly recursive folder that had been created by a java IDE, and I was unable to delete it in either Windows Explorer or at the cmd prompt.

However, when I installed git I also installed git-bash. In git-bash you can issue the command:

rm -rfd recursive_folder_name

and it will take care of it.

4
  • Important note: this can take a huge amount of time... Take a break, eat something, drink a coffee, and let Unix tool do its job ;) Surprisingly it works
    – Antwane
    Mar 5, 2018 at 9:34
  • also works if use a vmware based linux and mount the drive (if it is external ...) Oct 25, 2018 at 14:15
  • 1
    The fact that you need a Windows-ported Linux tool to cope with an NTFS shortcoming, is a clear sign of the stupidity of our era
    – Isaac
    Aug 21, 2020 at 16:20
  • Tried many suggestions, this was only worked that worked to delete a folder occupying some 365GB. I used MSYS2 rather than git-bash as I had that installed. And yes, it took an age, but it worked.
    – Ian
    Feb 16, 2022 at 18:22
4

It's possible that there's a Symbolic Link to a folder somewhere, most likely the second student in your path.

Try executing the command:

dir D:\Student\2012\user.name\servername

and see whether student is marked as DIR or SYMLINK/JUNCTION.

For example, this is a typical output of the dir command:

04/24/2012  10:58 PM    <DIR>          .
04/24/2012  10:58 PM    <DIR>          ..
04/24/2012  10:58 PM    <SYMLINKD>     a [h:\a]

You see that the first two entries are marked as DIR, while the third (a symbolic link to another folder) is marked as SYMLINKD. In your case (Windows 2003) I think there are JUNCTIONs instead of SYMLINKs.

If the student folder is a symbolic link or junction, run the commands:

cd /d D:\Student\2012\user.name\servername
rmdir student

and thus get rid of the recursion.

1
  • None of the directories are symlinks, I checked the first couple plus the one at the "end" where it gets stuck. Apr 24, 2012 at 20:27
2

It's possible that whomever was working in that folder manage to create a junction point that loops back up from user.name to servername, thus creating a the 'infinite loop' directory tree.

Junctions are a feature of the NTFS file system that lets you create a symbolic link to a directory that operates as an alias to the directory. http://en.wikipedia.org/wiki/NTFS_junction_point

You can see under the "Observed Effects" section of the wiki page the problem you describe.

Try running this utility to list junctions under that server:

http://technet.microsoft.com/en-us/sysinternals/bb896768

It should also give you the ability to delete the junction point.

2

I had a similar problem when eclipse created a massively deep folder tree which I couldnt delete. However since it was possible to move and rename folders I wrote a batch file to work it's way down the tree deleting folders one at at time and this worked! For a apparently infinately deep folder tree of FOLDERNAME in the C directory, i.e.

C:/*FOLDERNAME*/*FOLDERNAME*/*FOLDERNAME*/*FOLDERNAME*/*FOLDERNAME*/*FOLDERNAME*/...

Batch file :

:LoopStart
REN "C:/*FOLDERNAME*" "*TEMPFOLDERNAME*"
MOVE "C:/*TEMPFOLDERNAME*/*FOLDERNAME*" "C:/"
RMDIR /S /Q "C:/*TEMPFOLDERNAME*"
GOTO LoopStart
:LoopEnd

This works because TEMPFOLDERNAME is now a tree of finite length (a single folder with nothing in it) and the massive FOLDERNAME tree has one less folder every time the loop is run, eventually reaching the bottom by removing a folder at the top.

1

I had the same problem. The folder I wanted to delete contained an endless tree of links to the same name and every attempt to delete it caused Windows to throw a "File too long..." error. It failed regardless of the command I tried including all the above methods so I tried Cygwin's "rm" command in a cmd window just above the folder in question. I entered "rm -r -f [folder]" and the folder was deleted without complaint.

You can download Cygwin for free from https://cygwin.com/install.html. Its basically a UNIX shell for Windows that contains all the common commands (e.g., ls, rm, du, and cat) but also has a ton of packages that if all were downloaded would take up about 1GB of space. I believe the default download package includes the UNIX commands. If not, they're in the "utils" bundle if memory serves.

NOTE: You need not run Cygwin commands in its native bash shell window. They can be run in a cmd window by adding the path to cygwin's "bin" directory to your system PATH environmental variable. In my case I added "C:\cygwin64\bin".

I hope this helps.

1

I did run into the same issue with a 5000+ directory-deep folder mess that some Java application did and I wrote this:

https://gitlab.imanolbarba.net/imanol/DiREKT

It removed the whole thing after a while, but it managed to do the job, I hope it helps people who (as I), run into the same frustrating issue

1

Solved my endless looping directories with this Script:

:LoopStart
takeown /F "c:\temp\Application Data"
Attrib -S -H "c:\temp\Application Data"
REN "C:\temp\Application Data" "5"
takeown /F "c:\temp\5\Application Data"
Attrib -S -H "c:\temp\5\Application Data"
MOVE "c:\temp\5\Application Data" "C:\temp"
Del /F /S /Q c:\temp\5\*.*
RMDIR /S /Q "C:\temp\5"
GOTO LoopStart
:LoopEnd
0

I too had this, on a standalone Windows 10 system. C:\User\Name\Repeat\Repeat\Repeat\Repeat\Repeat\Repeat\Repeat seemingly to infinity.

As per the OP, I could navigate using Windows or Command Prompt to about the 50th one and no further. I could not delete it, or click on it, etc. exactly as described.

C is my language so eventually I wrote a program with a loop of system calls, which repeat until they fail. As per the DOS code from JohnO, I made a directory called tmp and moved Repeat\Repeat into that, deleted the now-empty Repeat folder, and moved tmp\Repeat back into current folder. Over and over again!

 while (times<2000)
 {
  ChkSystem("move Repeat\\Repeat tmp");
  ChkSystem("rd Repeat");
  ChkSystem("move tmp\\Repeat Repeat");
  ++times;
  printf("Removed %d nested so far.\n", times);
 }

ChkSystem just runs a system() call and checks the return value, stopping if it failed.

Importantly, it failed a number of times. I thought perhaps my program wasn't working, or that it was infinitely long after all. However, I have had this before with system calls, with things not syncing up, so I just ran the program again and it carried on from where it left off, so don't think your program isn't working. In total, after running it about 20 times, it cleared them all. In total, it was about 1280 folders deep originally. No idea what caused it. Crazy.

0

With 3-level looping directories:

:LoopStart
takeown /F "D:\temp\MyApp"
Attrib -S -H "D:\temp\MyApp"
REN "D:\temp\MyApp" "5"
takeown /F "D:\temp\5\MqttObserver"
Attrib -S -H "D:\temp\5\MqttObserver"
REN "D:\temp\5\MqttObserver" "6"
takeown /F "D:\temp\5\6\PackageFiles"
Attrib -S -H "D:\temp\5\6\PackageFiles"
REN "D:\temp\5\6\PackageFiles" "7"
MOVE "c:\temp\5" "C:\temp"
Del /F /S /Q c:\temp\5\*.*
RMDIR /S /Q "C:\temp\5"
MOVE "c:\temp\6" "C:\temp"
Del /F /S /Q c:\temp\6\*.*
RMDIR /S /Q "C:\temp\6"
MOVE "c:\temp\7" "C:\temp"
Del /F /S /Q c:\temp\7\*.*
RMDIR /S /Q "C:\temp\7"
GOTO LoopStart
:LoopEnd
1
0

I got it fixed by Running chkdsk /f through admin console, rebooted and then used file explorer delete. Though windows complained about the long file/folder something, it got rid of the problematic recursive folder once I confirmed to delete.

Not sure if there are other problems I might have created using the old chkdsk option in my Windows 7 Pro file system..

0

For me this variation of the Windows RoboCopy method worked:

mkdir c:\empty
robocopy c:\empty toolongdir /purge
rmdir c:\empty
rmdir toolongdir

Yes, it takes time but did the job.

0

I have the same problem here.

In other posts was suggested to mirror an empty folder to target folder trough network share with MS RoboCopy.

At this right moment i'm running this, with "> nul" to speedup (it does, really):

"C:\TEMP" is an empty folder and "W:\TMP" is the affected folder:

robocopy /mir c:\temp \\127.0.0.1\w$\tmp > nul

Edited for a clear answer

1
  • 1
    Welcome to Super User. If you're citing a solution from elsewhere, please include attribution and a link for further reading. Answers are intended to be definitive solutions. If you're still in the process of trying this, it might be premature to post.
    – fixer1234
    Aug 9, 2016 at 1:45
-1

Simply run MS ROBOCOPY as follows:

  1. Create an "empty folder" with the same name as the folder that contains the "infinitely recurring directory tree".
  2. Run the ROBOCOPY from the command line with the /MIR option.
  3. After ROBOCOPY ends (and it might take a few seconds),
  4. Delete the folder with the "infinitely recurring directory tree" which is now an "empty folder".
  5. Also go ahead and delete the "empty folder" you created in Step 1.

. . . . . . . . . . . . . . .

ROBOCOPY "C:\anyplace\BADFOLDER" "C:\wherever\BADFOLDER" /MIR

Where:

C:\anyplace\BADFOLDER .. is the "empty folder" you created, and

C:\wherever\BADFOLDER .. is the folder where the "infinitely recurring directory tree" is.

. . . . . . . . . . . . . . .

After ROBOCOPY runs .. You now have two "empty" BADFOLDERS.

To clean-up, delete both

C:\anyplace\BADFOLDER and C:\wherever\BADFOLDER

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .