Friday, September 23, 2005

NTFS, Windows Explorer, .NET, and Deep Paths

The NTFS folder tree is just a linked-list type of structure. Each node (folder) has a parent. To move a folder, all you need to do is change the pointer to the parent folder.

That's all well and good, but it seems that you can have really deep folder structures with little consequence to NTFS. Again, that's all well and good, but our friend Windows Explorer seems to have a problem representing, or rather accessing, deep folder structures due to a limit on how long a string path can be.

So, we came upon a problem today where a directory sync program was incorrectly configured, and as a result, created a really deep folder chain. For instance, it synchronized the folders from another machine (B -> A), but the next pass, it copied subfolders not into the same parent as previously, but into one of the subfolders that was previously created (B -> B). Let this run for 1000 passes, or so, and you get 1000 subdirectories. Blah!

How do you go about deleting these? Windows Explorer and the CMD shell was no help--the string path to the end of the chain was simply too long.

I found a manual method that worked, and surprisingly, did not require that much time.

Given:

Dir
--Dir2
----Dir3
------Dir4
--------Dir5
----------Dir6
------------Dir7
--------------Dir8
----------------...

Navigate as far into the tree as possible using Windows Explorer (i.e., maybe only to Dir6) and move that folder into the parent (Dir). This is just a parent pointer change to NTFS.

New directory tree might resemble:

Dir
--Dir2
----Dir3
------Dir4
--------Dir5
--Dir6
----Dir7
------Dir8
--------…

Keep repeating with the longest chain that remains (i.e., navigate into the new Dir 6 chain until you hit the path limit in Explorer, then copy that folder into the root).

When you have nothing but a bunch of smaller chains, you can then delete them all at the same time (i.e., delete everything inside of “Dir”).

BTW: It seems that the .NET Framework classes (System.IO namespace) simply use Windows Explorer, because they seemed to be subject to the very same limitations.