Find Duplicates in Subfolders

FindDupesInSubfolders lets you find duplicate files stored in the same folder within a directory structure. It works by enumerating the selection and running Find non-recursively on every folder. The results are returned as a dated collection.

How to setup and use

:one: Save CommandFindDupesInSubfolders.js.txt to

%appdata%\GPSoftware\Directory Opus\Script AddIns

:two: Add the new command to a button, hotkey, context menu, etc. like any built-in command. It can also be run from the FAYT Command field.

2023-07-04 - 19.06.13

:three: Select the folders you want to get checked and run FindDupesInSubfolders on them.

:four: After the command has finished make sure the correct collection is selected in the panel, otherwise you may be unable to select and delete files.

2023-07-04 - 19.07.26

Things you might enjoy reading

How to use buttons and scripts from this forum

The script's inner workings

JScript
function OnInit(initData) {
    initData.name = 'FindDupesInSubfolders';
    initData.version = '2023-07-04';
    initData.url = 'https://resource.dopus.com/t/find-duplicates-in-subfolders/44909';
    initData.desc = '';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'FindDupesInSubfolders';
    cmd.method = 'OnFindDupesInSubfolders';
    cmd.desc = '';
    cmd.label = '';
    cmd.template = '';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnFindDupesInSubfolders(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var tab = scriptCmdData.func.sourcetab;
    var lst = tab.lister;
    var fsu = DOpus.FSUtil();
    var ssi = DOpus.Create().StringSetI();

    cmd.deselect = false;

    if (tab.selected_dirs.count == 0) return;

    cmd.RunCommand('Set UTILITY=otherlog');
    DOpus.ClearOutput();

    DOpus.Output('Enumerating selection...');

    for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (String(item.realpath).indexOf('$RECYCLE.BIN') >= 0) continue;
        ssi.insert(item);

        var folderEnum = fsu.ReadDir(item, 'r');
        while (!folderEnum.complete) {
            var folderItem = folderEnum.Next();
            if (!folderItem.is_dir) continue;
            ssi.insert(folderItem);
        }
        folderEnum.Close();
    }

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

    var k = ssi.count;
    var newColl = fsu.GetItem('coll://Dupes-' + DOpus.Create().Date().Format('D#yyyyMMdd-T#HHmmss'));
    cmd.RunCommand('CreateFolder NAME="' + newColl + '" READAUTO=tab,dual');

    DOpus.Output('');
    DOpus.Output('Searching ' + k + ' folders...');
    DOpus.Output('');

    for (var e = new Enumerator(ssi); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var cmdLine = 'Find' +
            ' NAME=*.*' +
            ' DUPES' +
            ' MD5=cache' +
            ' DELMODE' +
            ' COLLNAME="' + newColl.name + '"' +
            ' IN="' + item + '"';

        DOpus.Output(k + '\t\t' + cmdLine);
        cmd.RunCommand(cmdLine);
        k--;
    }

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

    lst.Update();
    cmd.SetSourceTab(lst.desttab);
    cmd.RunCommand('Set SORTBY=parent,-name');
    cmd.RunCommand('Set CHECKBOXMODE=on');
    cmd.RunCommand('Set GROUPBY=dupes');
    cmd.RunCommand('Set UTILITY=dupe');
}
3 Likes

Hello @lxp, could you help me find duplicate files in different subfolders within the same selected folder?

For example:

Selected folder

   Subfolder 1
         File 1
         File 240

   Subfolder 2
         File 2
         File 240

That it is recognized that the file "File 240" is duplicated even if it is located in different subfolders.

I tried to change tab.selected_dirs by tab.all

on the line:
for (var e = new Enumerator(tab.selected_dirs); !e.atEnd(); e.moveNext()) {
but it didn't work.

Thank you very much for any help or guidance

Yes. Don't use this script :smiley:

The Find panel will do the job.

Sorry, I thought I wouldn't be asking you for such a difficult thing.