Duplicate and Rename

This button should work. The pattern matching can be made stricter, if needed. Uncomment the last line, if you like the results.

// https://resource.dopus.com/t/duplicate-and-rename/39142

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

    cmd.deselect = false;

    var re = /(.*-)(0?\d+)$/;

    cmd.RunCommand('Set UTILITY=otherlog');
    DOpus.Output('Enumerating...\n');
    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var stem = item.name_stem;
        if (!re.test(stem)) continue;
        var oldStart = stem.replace(re, '$1');
        var oldNum = stem.replace(re, '$2');
        var newNum = Number(oldNum) + 1;

        var cmdLine = 'Copy DUPLICATE FILE="' + item + '" AS="' + oldStart + newNum + item.ext + '"';
        DOpus.Output(cmdLine);
        // cmd.RunCommand(cmdLine);
    }
    DOpus.Output('\n... done.');
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>Copy and Increment</label>
	<icon1>#copy</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/duplicate-and-rename/39142</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction />
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    var re = /(.*-)(0?\d+)$/;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    DOpus.Output(&apos;Enumerating...\n&apos;);</instruction>
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var stem = item.name_stem;</instruction>
		<instruction>        if (!re.test(stem)) continue;</instruction>
		<instruction>        var oldStart = stem.replace(re, &apos;$1&apos;);</instruction>
		<instruction>        var oldNum = stem.replace(re, &apos;$2&apos;);</instruction>
		<instruction>        var newNum = Number(oldNum) + 1;</instruction>
		<instruction />
		<instruction>        var cmdLine = &apos;Copy DUPLICATE FILE=&quot;&apos; + item + &apos;&quot; AS=&quot;&apos; + oldStart + newNum + item.ext + &apos;&quot;&apos;;</instruction>
		<instruction>        DOpus.Output(cmdLine);</instruction>
		<instruction>        // cmd.RunCommand(cmdLine);</instruction>
		<instruction>    }</instruction>
		<instruction>    DOpus.Output(&apos;\n... done.&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

https://resource.dopus.com/t/how-to-use-buttons-and-scripts-from-this-forum/3546

2 Likes