Rename using Template

Take a deep breath and try

// https://resource.dopus.com/t/rename-using-template/41439

// 2022-06-10

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

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

    var item = tab.selected(0);

    var part1 = item.name_stem.replace(/(.*_)(\d+)/, '$1');
    var part2 = item.name_stem.replace(/(.*_)(\d+)/, '$2');

	var cmdLine = ('Rename ADVANCED PATTERN="*" TO="' + part1 + '[#]" NUMBER=' + part2);
    // DOpus.Output(cmdLine);
    cmd.RunCommand(cmdLine);
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>41439</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/rename-using-template/41439</instruction>
		<instruction />
		<instruction>// 2022-06-10</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    if (tab.selected.count == 0) return;</instruction>
		<instruction />
		<instruction>    var item = tab.selected(0);</instruction>
		<instruction />
		<instruction>    var part1 = item.name_stem.replace(/(.*_)(\d+)/, &apos;$1&apos;);</instruction>
		<instruction>    var part2 = item.name_stem.replace(/(.*_)(\d+)/, &apos;$2&apos;);</instruction>
		<instruction />
		<instruction>	var cmdLine = (&apos;Rename ADVANCED PATTERN=&quot;*&quot; TO=&quot;&apos; + part1 + &apos;[#]&quot; NUMBER=&apos; + part2);</instruction>
		<instruction>    // DOpus.Output(cmdLine);</instruction>
		<instruction>    cmd.RunCommand(cmdLine);</instruction>
		<instruction>}</instruction>
	</function>
</button>

Don't worry, the preview window will allow you to cancel, if you don't like the results :slight_smile: