Select files in Destination that START with the filenames in Source

I am wondering if there is an easy way to do this:
Source contains
base.zip
notbase.zip

Destination contains
base-test.jpg
base-more.jpg
base.jpg
notbase-test.jpg
somethingelse.jpg

I am looking for a command that selects all "FILENAME*.*" from Source to Destination. So it would in my example select all but somethingelse.jpg. (and actually I want to invert that selection in the end because my final goal would be to delete somethingelse.jpg - all files in the destination that dont have a "stub" filename in source). Hope this is understandable. Love all the special selects we already have, but can't see how to do this without a complicated script.

Cheers, Boris

1 Like

Would a simple script be ok? :wink:

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dtab = clickData.func.desttab;

    cmd.deselect = false;
    cmd.SetSourceTab(dtab);
    
    cmd.RunCommand('Select NONE');
    
    for (var e = new Enumerator(tab.files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        cmd.RunCommand('Select PATTERN="' + item.name_stem + '*.*"');
    }
    
    cmd.RunCommand('Select INVERT');
    cmd.RunCommand('Select SHOWFOCUS');
    cmd.RunCommand('Set SOURCE=toggle');
}

Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Select Files in Dest</label>
	<icon1>#advancedselect</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var dtab = clickData.func.desttab;</instruction>
		<instruction />
		<instruction>    cmd.deselect = false;</instruction>
		<instruction>    cmd.SetSourceTab(dtab);</instruction>
		<instruction>    </instruction>
		<instruction>    cmd.RunCommand(&apos;Select NONE&apos;);</instruction>
		<instruction>    </instruction>
		<instruction>    for (var e = new Enumerator(tab.files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        cmd.RunCommand(&apos;Select PATTERN=&quot;&apos; + item.name_stem + &apos;*.*&quot;&apos;);</instruction>
		<instruction>    }</instruction>
		<instruction>    </instruction>
		<instruction>    cmd.RunCommand(&apos;Select INVERT&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select SHOWFOCUS&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Set SOURCE=toggle&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

</button>
How to

How to use buttons and scripts from this forum

Thank you - does not work though, all files in Destination are selected. Will try to find out why. And found out already - does not work when source filename has special characters (in my case there are () in the filenames). Let me see if I can fix that.

Adding REGEXP to the Select Pattern command does the trick for me.

...unless my filename has TWO parantheses "...(USA) (EN)". But apparently once I use wildcards I am stuck.

If using wildcards, put a ' (single quote) character before all brackets.

If using regex,. put a \ (backslash) before them. (Remember JScript needs \ to be escaped as well, so it'll usually be a \\ in script code.)

(If using regex, the *.* part in the original script will be wrong, of course. I'm assuming you've changed that. Also note that *.* will only match files with extensions, unlike MS-DOS-style wildcards which would match things with no extension as well. Use just * if you don't require an extension.)

I thing the solution is to use the wild.escapestring method. Just trying to find the right syntax. The input is a list of filenames - so I can't put the escape character in there by fixed positions.

Leo / lpx, I can't find any script samples that use the wild.escapestring method and all the hacks I try do not work. Any idea on how to take the string "item", use the method on it (or create a 2nd scrint) and then use the .name_stem on it?

Example:

var w = DOpus.FSUtil.NewWild;
var n = "Example (Name).txt";

var ew = w.EscapeString(n);
DOpus.Output(ew);

var er = w.EscapeString(n, "r");
DOpus.Output(er);

Output:

Example '(Name').txt
Example \(Name\)\.txt

There's another example here (although you don't want the "b" mode in your situation):

Try this update:

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dtab = clickData.func.desttab;
    var fsu = DOpus.FSUtil();
    var wld = fsu.NewWild();

    cmd.deselect = false;

    if (!dtab) return;
    cmd.SetSourceTab(dtab);

    cmd.RunCommand('Select NONE');

    for (var e = new Enumerator(tab.files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        cmd.RunCommand('Select PATTERN="' + wld.EscapeString(item.name_stem) + '*.*"');
    }

    cmd.RunCommand('Select INVERT');
    cmd.RunCommand('Select SHOWFOCUS');
    cmd.RunCommand('Set SOURCE=toggle');
}
1 Like

Ah, that was what I missed. THANK YOU BOTH! This is awesome!