WslFind (Find files with Linux)

WslFind makes the WSL (Windows Subsystem for Linux) Find command available in Opus.

WslFind supports two arguments:

EXPRESSION is the search string that will be handed over to Find. If the string contains spaces, it must be put in quotes. Quotes in the string must be escaped with a quote.

PATH determines the paths that will be searched. If omitted, the currently selected folders will be searched. If no folder is selected, the path from the current tab will be used.

Run

wsl find -help

from a command prompt to get the details about the parameters.

The results are returned as a dated collection. The collection's description will show the search expression.

Examples

WslFind EXPRESSION="-name *.doc"
WslFind EXPRESSION="-name *.doc" PATH D:\test E:\Books
WslFind "-name ""01 *.doc"""

How to set up and use

:one: Save CommandWslFind.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, or run it from the FAYT Command field.

:three: Optional: Toggle the description column with

Set COLUMNSTOGGLE="desc(!,a,0)"

or

Set COLUMNSTOGGLE="userdesc(!,a,0)"

Things you might enjoy reading

How to use buttons and scripts from this forum
What is Windows Subsystem for Linux | Microsoft Learn
find Man Page - Linux - SS64.com

The script's inner workings

JScript
function OnInit(initData) {
    initData.name = 'WslFind';
    initData.version = '2023-11-15';
    initData.copyright = '';
    initData.url = 'https://resource.dopus.com/t/wslfind-find-files-with-linux/46976';
    initData.desc = 'Find files with WSL find command. More info: wsl find -help';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAddCommands(addCmdData) {
    var cmd = addCmdData.AddCommand();
    cmd.name = 'WslFind';
    cmd.method = 'OnWslFind';
    cmd.desc = '';
    cmd.label = '';
    cmd.template = 'expression,path/k/m';
    cmd.hide = false;
    cmd.icon = 'script';
}

function OnWslFind(scriptCmdData) {
    var cmd = scriptCmdData.func.command;
    var args = scriptCmdData.func.args;
    var tab = scriptCmdData.func.sourcetab;
    var fsu = DOpus.FSUtil();
    var stt = DOpus.Create().StringTools();

    cmd.deselect = false;

    if (args.got_arg.path) {
        var itemsToEnum = args.path;
    } else {
        var itemsToEnum = tab.selected_dirs.count > 0 ? tab.selected_dirs : DOpus.Create().Vector(tab.path);
    }

    var strPath = '';
    for (var e = new Enumerator(itemsToEnum); !e.atEnd(); e.moveNext()) {
        var wslPath = GetWslPath(fsu.Resolve(e.item()));
        if (!wslPath) continue;
        if (strPath) strPath += ' ';
        strPath += '"' + wslPath + '"';
    }

    if (!strPath) {
        DOpus.Output('Path missing or invalid.');
        return;
    }

    var strExpression = args.expression;

    var exportFile = fsu.GetTempFilePath();

    cmd.Clear();
    cmd.SetType('msdos');
    cmd.SetModifier('runmode', 'hide');

    var cmdLine = 'wsl find' +
        ' ' + strPath +
        ' ' + strExpression +
        ' > "' + exportFile + '"';

    DOpus.Output(cmdLine);
    cmd.RunCommand(cmdLine);

    var fileList = stt.Decode(fsu.GetItem(exportFile).Open().Read(), 'utf8').replace(/(^|\n)\/mnt\/(\w)/g, '$1$2:');

    var importFile = fsu.GetTempFile();
    if (fileList != '') importFile.Write(fileList);
    importFile.Close();

    var newColl = 'coll://WSL-' + DOpus.Create().Date().Format('D#yyyyMMdd-T#HHmmss');
    cmd.RunCommand('CreateFolder NAME="' + newColl + '"');
    cmd.RunCommand('SetAttr' +
        ' FILE="' + newColl + '"' +
        ' DESCRIPTION="' + strExpression.replace(/"/g, '""') + '"');

    cmd.ClearFiles();
    cmd.AddFilesFromFile(importFile, 'utf8');
    cmd.SetDest(newColl);
    cmd.RunCommand('Copy COPYTOCOLL=member');
    cmd.RunCommand('Go' +
        ' PATH="' + newColl + '"' +
        ' OPENINDUAL' +
        ' TOFRONT');
}

function GetWslPath(item) {
    var tmp = String(item);
    if (tmp.charAt(1) == ':') {
        return '/mnt/' + tmp.charAt(0).toLowerCase() + tmp.substring(2).replace(/\\/g, '/');
    } else {
        return '';
    }
}
3 Likes