Folder Cleaner : Delete folders but keep files

Folder Cleaner is a script that deletes folders but keeps the files in them.You can also choose whether to recursively process subfolders.

(Of course, there may be other simpler ways to achieve this requirement:)

How to use

  • Method 1: Paste the script code directly

Use the custom new button to edit the button, change the type of Function definition to Script Function, and the script type to JScript. Finally, paste the following script code in.

function OnClick(clickData) {
    var dlg = clickData.func.Dlg;
    dlg.message = "Are you sure you want to delete the selected folder but keep all files?";
    dlg.title = "Folder Delete Confirm";
    dlg.icon = "question";
    dlg.options(0).label = "Recursively process subfolders";
    dlg.options(0).state = true;
    dlg.options(1).label = "Show delete confirmation";
    dlg.options(1).state = true;
    dlg.buttons = "OK|Cancel";

    var result = dlg.Show();

    if (result === 1) {
        var cmd = clickData.func.command;
        var tab = clickData.func.sourcetab;
        var items = tab.selected_dirs;
        cmd.deselect = false; 

        for (var e = new Enumerator(items);!e.atEnd(); e.moveNext()) {
            var folder = e.item().realpath;
            if (dlg.options(0).state) {
                // If "Recursively process subfolders" is checked, perform recursive operation
                if (!moveFilesRecursively(folder, folder, dlg)) {
                    DOpus.Output("Error in recursive processing.");
                    break;
                }
            } else {
                // If not checked, perform non-recursive operation
                if (!moveFilesNonRecursively(folder)) {
                    DOpus.Output("Error in non-recursive processing.");
                    break;
                }
            }

            // Determine the parameters for delete command based on confirmation option
            var deleteParams = dlg.options(1).state? '' : 'QUIET';

            // Finally delete the original folder with appropriate parameters
            if (!cmd.RunCommand('Delete NORECYCLE SKIPNOTEMPTY FILE="' + folder + '" ' + deleteParams)) {
                DOpus.Output("Failed to delete folder.");
            }
        }
    } else {
        DOpus.Output("Operation canceled.");
    }
}

function moveFilesRecursively(currentFolderPath, rootFolderPath, dlg) {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var folder = fso.GetFolder(currentFolderPath);
    var cmd = DOpus.Create.Command();

    // Iterate over all files in the current folder and move them to the parent directory of the root folder
    var files = new Enumerator(folder.Files);
    for (;!files.atEnd(); files.moveNext()) {
        var file = files.item();
        if (!cmd.RunCommand('Copy MOVE FILE="' + file.Path + '" TO="' + rootFolderPath + '\\.."')) {
            return false;
        }
    }

    // Recursively process subfolders
    var subfolders = new Enumerator(folder.SubFolders);
    for (;!subfolders.atEnd(); subfolders.moveNext()) {
        var subfolder = subfolders.item();
        if (!moveFilesRecursively(subfolder.Path, rootFolderPath, dlg)) {
            return false;
        }
    }

    // Delete the empty folder after processing subfolders if it is not the root folder
    if (currentFolderPath!== rootFolderPath) {
        var deleteParams = dlg.options(1).state? '' : 'QUIET';
        if (!cmd.RunCommand('Delete NORECYCLE SKIPNOTEMPTY FILE="' + currentFolderPath + '" ' + deleteParams)) {
            return false;
        }
    }

    return true;
}

function moveFilesNonRecursively(folderPath) {
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var folder = fso.GetFolder(folderPath);
    var cmd = DOpus.Create.Command();

    // Move all files in the current folder to the parent directory
    if (!cmd.RunCommand('Copy MOVE FILE="' + folderPath + '\\*" TO="' + folderPath + '\\.."')) {
        return false;
    }

    return true;
}
  • Method 2: Install the script package

Download the script package and run it directly. This will add a script (which can be configured and viewed through the Script Management) and a command to Directory Opus. Finally, drag the command to any location in the Customize > Commands interface (or other methods).

Folder Cleaner.opusscriptinstall (2.2 KB)
version 1.1

  • Fixed the issue of folders with the same name being deleted in non-recursive mode;
  • Added a confirmation deletion option;
3 Likes

With the delete command it's running, it might be worth adding the NORECYCLE SKIPNOTEMPTY arguments so it won't delete folders that still contain things.

If I've understood the script correctly, that can happen in the "non-recursive" case if a folder has another folder with the same name below it.

Thanks a lot, I overlooked this. I've updated it with your suggestion.

1 Like

I use this, but it doesn't work for subfolders. Maybe someone can use this as a base, and add subfolders:

@dirsonly 
Copy MOVE FILE={filepath$}* TO={filepath|..}
Delete FILE={filepath$|noterm} NORECYCLE SKIPNOTEMPTY QUIET

It seems to work OK with subfolders here.

Can you give more detail on what you're doing and what goes wrong?

By "subfolders" do you mean folders inside expanded folders, or something else?

Are you selecting just one folder which you want to move things out of, or multiple folders and/or contents as well?

Here's an example video. My goal is to "zap" C:\0000, and get all text files (001.txt, 002.txt, 003.txt, 004.txt) into C:\0000, while deleting all subfolders. To do this currently, I have to zap each subfolder inside C:\0000 as they zap out. I was planning one day to make the code zap out all files, and delete all subfolders, no matter how deep.

The command you're using just moves things up one level, which is what I think is happening correctly in your video.

If you want to flatten everything recursively and remove all subdirectories while keeping the files, that command is the wrong thing to use. The post at the top of this thread has a script for doing that. (I think there are some other methods on the forum as well, if that one doesn't work for you.)

(And now I'm wondering why this was asked in this thread, as it doesn't seem to be about the script the thread is about.)

1 Like

I use Flat View (Mixed) then move the files into one folder. Then I delete the empty folders. That works well even with over 100,000 files.

1 Like