Massive amount of subfolders and long subfolders. How to delete all of them

deletingdirectoryfilesrecursive

We have a little problem here.

We have a share with the backup of all the server's offices, Its a really big share with more than 8.000.000 files.

Our users usually give long names to the folders they create, and then make subfolders (long too) and more subfolders… and more suboflders….

We have a new share with more capacity, and with a simpe robocopy bat we copied all the files and folders (some give problems, but we manually copied them)

But the problem is deleting them. del command didnt work well when so long paths, neirder rmdir… I'm tried some commanders, but no luck.

Can u recommend me any tool that can delete recursively or able to delete 255+ paths?

Edited: The SO on background of the share it's NetApp OS. But I can access it from Windows Servers. 2000 and 2003

Thanks.

Not perfect solution but that works

Here's the script on Windows Scripting I do to solve the problem. It isn't perfect but if anyone has the same problem can use it.

Option Explicit 
DIM strFolder
DIM objFSO

' ************************************************************
' Setup
' ************************************************************
' Folder to delete files from (files will also be deleted from subfolders)
strFolder = "Z:\del"
' ************************************************************
set objFSO = createobject("Scripting.FileSystemObject")

Wscript.echo "Processing " & strFolder
RecursiveDeleteByExtension strFolder

wscript.echo "Finished"

sub RecursiveDeleteByExtension(byval strDirectory)
    DIM objFolder, objSubFolder, objFile, Tmp, Indice

    set objFolder = objFSO.GetFolder(strDirectory)

    Wscript.echo "Processing " & strDirectory

    for each objFile in objFolder.Files
        WScript.echo "Deleting:" & objFile.Path
        objFile.Delete
    next    
    Indice = 0
    For each objSubFolder in objFolder.SubFolders
        If Len (objSubFolder.Name) > 5 Then
            Indice = Indice + 1
            objSubFolder.Move(objFolder.Path & "\" & Indice & ".t")
        End if
    Next

    for each objSubFolder in objFolder.SubFolders
        RecursiveDeleteByExtension objSubFolder.Path
    Next
    objFSO.DeleteFolder(strDirectory)
end sub

What this script do, recursively, is changing a very long path like \bla…\bla…\bla…\bla… to a much more shorter \1\2\1\2\ and after the renaming in the end of each recursion, it deletes the object folder (who's empty, btw).

It worker extremely well for me, even so we found full paths near 200 chars (imagine with before the script).

Best Answer

This is a program dot_deltree.cs in C# which deletes directory trees of any depth. It works by first moving too deep directories to random name in the most shallow directory.

using System;
using System.IO;

public class dot_deltree
{
    public static void Main(string[] args) {
        if ( ! (args.Length == 1) ) {
            Console.Error.WriteLine("Usage: dot_deltree [path]");
            Environment.Exit(1);
        }

        string dirname = args[0];

        if ( ! Directory.Exists(dirname) ) {
            Console.Error.WriteLine("{0} does not exist or is not a directory!", dirname);
            Environment.Exit(1);
        }

        confirm_deleting(dirname);

        while ( true ) {
            string too_deep_dir = deltree( dirname );
            if ( too_deep_dir.Equals("") ) {
                break;
            }
            string randomname = Path.GetRandomFileName();
            Console.Error.WriteLine(
                "Moving too deep directory {0}:{2} to random name {1}", 
                too_deep_dir, randomname, too_deep_dir.Length
            );
            Directory.Move( too_deep_dir, Path.Combine(dirname,randomname) );
        }
    }

    public static void confirm_deleting(string path) {
        Console.Write("Do you really want do delete directory {0} recursively (type YES)? ", path);
        string result = Console.ReadLine();
        if ( ! result.Equals("YES") ) {
            Environment.Exit(1);
        }
    }

    public static string deltree(string uncpath) {
        if ( uncpath.Length > 200 ) {
            return uncpath;
        }
        string[] subdirectories = Directory.GetDirectories(uncpath);
        foreach (string subdirectory in subdirectories) {
            string result = deltree(subdirectory);
            if ( ! result.Equals("") ) {
                // Return up too deep directory
                return result;
            }
        }
        // Console.Error.WriteLine("Deleting {0}", uncpath);
        Directory.Delete(uncpath,true);
        return "";
    }
}

Compiled using Mono C# compiler using gmcs dot_deltree.cs for .NET 2.0 is here (4kb).