How to Auto Delete Specific Subfolders While Skipping Root Folders

Hello,

Sorry, I tried but I can't wrap my head around how to do this correctly.

I want to run DELETE FORCE QUIET SECURE (from a button) at a specific location, example %userprofile%\Documents\Test\

I need to skip all files and folders in \Test\ while deleting their subfolders if called sub1 sub2 and all their content. Examples:

\Test\1\sub1 <Deleted
\Test\1\sub2 <Deleted
\Test\1\sub4 <Left Alone
\Test\123\sub1 <Deleted
\Test\ABC\sub1 <Deleted
\Test\ABC\sub9 <Left Alone

Just the sub folders sub1 sub2 need to be deleted. If the parent folder is now empty, it can remain empty and undeleted.

Can anyone help me with this one please?

I expected this to work, but it doesn't. Are folders somehow immune to deletion here?

Delete FILTERDEF fullpath match "%userprofile%\\Documents\\Test\\*\\(sub1|sub2)"

Hi lxp,

I'm assuming your question is for John or Leo.

I too can not run it. Inside the editor hitting "Run" or outside of cusomize, the button becomes deactivated so one can not click on it.

Selecting the Test folder enables the button. Adding \\* to the filter definition allows the button to delete all files in the folders. However, deleting the sub1 and sub2 folders seems more complicated.

Although a script could easily handle the entire process, I'm not ready to wave the white flag and admit defeat just yet! :wink:

Ahh. I assumed since the path was listed, it wouldn't need to be selected.

I'm hoping to run it without having to first select anything. Sorry, I should of mentioned that.

I have a button for clearing history from lots of different locations so I'll be adding it to that. That button already runs using /aliases %systemvariables% and direct paths

You can pass the path via FILE.

Delete FILE="%userprofile%\Documents\Test" FILTERDEF fullpath match "%userprofile%\\Documents\\Test\\*\\(sub1|sub2)"
1 Like

Understood. Thanks.

We still have the problem of trying to delete specific sub folders, right?

Yes

1 Like

The filter works fine for Find. So you could in step one find the folders, and in step two delete them.

Thanks for the help so far. Could you show me a screenshot of how you have your Find definded?

It would look like this:

Find SHOWRESULTS=source,tab IN "/profile\Documents\Test" FILTERDEF fullpath match "/profile\\Documents\\Test\\*\\(sub1|sub2)"

Side note: %userprofile% can be used in the Find panel, but the Find command requires /profile. Is this a bug?

1 Like

Here's the script I mentioned: Del53513 (please find a better name!).

It will list the folders it is going to delete. Run it with the switch DRYRUN to only list the folders without actually deleting them:

Del53513 DRYRUN

Let me know if you need help integrating the other folders you are cleaning up.


Save CommandDel53513.js.txt to   ↓

%appdata%\GPSoftware\Directory Opus\Script AddIns
JScript
function OnInit(initData) {
    initData.name = 'Del53513';
    initData.version = '2024-11-19';
    initData.url = 'https://resource.dopus.com/t/how-to-auto-delete-specific-subfolders-while-skipping-root-folders/53513';
    initData.desc = 'Del53513';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'Del53513';
    cmd.method = 'OnDel53513';
    cmd.desc = 'Del53513';
    cmd.label = 'Del53513';
    cmd.template = 'dryrun/s';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnDel53513(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var args = scriptCmdData.func.args;
    var fsu = DOpus.FSUtil();
    var wld = fsu.NewWild();

    var srcPath = fsu.Resolve('%userprofile%\\Documents\\Test');
    DOpus.Output('srcPath:');
    DOpus.Output(srcPath);
    DOpus.Output('');

    var srcRE = '\\[^\\]+\\(sub1|sub2)$';

    var re = new RegExp(wld.EscapeString(srcPath, 'r') + wld.EscapeString(srcRE, 'b'), 'i');
    DOpus.Output('re:');
    DOpus.Output(re);
    DOpus.Output('');

    DOpus.Output('Enumerating...' + (args.dryrun ? ' (dry run)' : ''));
    DOpus.Output('');

    cmd.ClearFiles();

    var folderEnum = fsu.ReadDir(srcPath, 'r');
    while (!folderEnum.complete) {
        var folderItem = folderEnum.Next();
        if (!folderItem.is_dir) continue;
        if (!String(folderItem).match(re)) continue;
        DOpus.Output(folderItem);
        cmd.AddFile(folderItem);
    }
    folderEnum.Close();

    DOpus.Output('');
    DOpus.Output('... done.');
    DOpus.Output('');

    var cmdLine = 'Delete';
    // var cmdLine = 'Delete FORCE QUIET SECURE';
    DOpus.Output(cmdLine);

    if (!args.dryrun) cmd.RunCommand(cmdLine);
}
2 Likes

Both solutions work great! On the script, thanks for adding in a dryrun switch for testing.

Thank you so much for you help with this, Alex! I really appreicate your time and effort you put into this for me.