Monday, March 12, 2012

Infinite Windows Directory Solution

WOW. I just got finished taking care of one of the most annoying Windows bugs I've seen yet. Some weird behavior involving Netbeans created a MASSIVE directory tree with repeating directory names (e.g. myDir/build/classes/build/classes/build/classes...). When I tried to delete this tree with any ordinary method, I'd get an error message, "Path name too long...yada yada." Windows Explorer would show that it was gone, but I'd come back and it was still there.

Nevertheless, HERE IS THE SOLUTION: Create a batch file that follows these steps.

  1. Rename your repeating directories to an x.
  2. Dive into the renamed directory and try to move the alternate directory up into your top level directory (i.e. my_Dir).
  3. Remove the directory that you renamed (which no longer contains anything) Make sure to use the /s/q options (/s removes any subfiles, /q prevents windows from asking you "Y/N?" each time you delete the folder).
  4. Repeat by calling the .bat (batch) file again at the end of the batch file. (YES, this creates an infinite loop. You'll just need to stop the batch file with "ctrl+c".

ren "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\build" x
ren "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\classes" x

move "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\x\build" "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\"
move "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\x\classes" "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\"

rd "C:\Users\valued customer\Documents\School Stuff\CS 340\Homework_Docs\remove_me\x" /s /q



  1. Thank you very much for this. Had the same problem and spent hours on searching for some stupid tool to do this job. Each tool failed. Adapting your code solved the problem easily.

  2. Giel van Altena

    Took me 10 minutes to think about it, but it worked. Create a batch file with notepad, adapt his code to your own file location and run the batch file. Thanks.