Duplicate and Rename

I've been searching Opus' documents for several hours, but couldn't find how to do this.

I'm trying to add a button that duplicates the selected file in a lister and renames it (in the same folder)

There's a pattern in the file names:
Alphabet & 2 digits - 2 digits - 2 digits - 4 digits
Example of file names:
K01-12-36-0127
T08-32-01-1259
.......

The rename is also patterned. The right 4 digits plus 1.
K01-12-36-0127 should be copied and renamed to ---> K01-12-36-0128
T08-32-01-1259 should be copied and renamed to ---> T08-32-01-1260

I used COPY DUPLICATE, but each time I have to type in the new file name in a prompt box.

Is there any way to do it automatically?

thank you.

You'll need to use scripting if you want to apply maths to the old filename to generate the new one. A regex could be used to split the old filename up:

var n = "K01-12-36-0128";
var re = /^(.*-)(\d\d\d\d)$/
var m = n.match(re);
if (m)
{
	var mainParts = m[1];
	var lastPart = parseInt(m[2],10) + 1;
	lastPart = ZeroPad(lastPart,4);
	var nn = mainParts + lastPart;
	DOpus.Output(nn);
}

function ZeroPad(s, n)
{
	s = s + "";
	while(s.length < n)
	{
		s = "0" + s;
	}
	return s;
}
1 Like

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

@lxp thanks for the script.
I pasted it into a button, selected a file and clicked the button. I received a message that the file has been copied, but actually it's not.

Even if the file had been copied and renamed, the result is not what I expect. After the rename it should be K01-21-01-0009

Any further assist is appreciated.
Thanks.

@Leo I appreciate your time and trying to help. But unfortunately I couldn't implement it into a button that can do the job.
How can I use this script to copy and rename a selected file by clicking a button.?
Thank you.

"Uncomment the last line, if you like the results."

You can use the ZeroPad function in my script above to fix that part.

My script only showed how to do the unusual part, i.e. changing the filename. But how to do the rest is shown in Lxp's script.

1 Like

It's really my first attempt in JS.
I ended up with the following.
While it works, any correction is much appreciated.

// 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 = /^(.*-)(\d\d\d\d)$/

 
    var e = new Enumerator(tab.selected_files);
    var item = e.item();
    var stem = item.name_stem;

	var m = stem.match(re);

	if (m)
	{
		var mainParts = m[1];
		var lastPart = parseInt(m[2],10) + 1;
		lastPart = ZeroPad(lastPart,4);
		var nn = mainParts + lastPart;
		var cmdLine = 'Copy DUPLICATE FILE="' + item + '" AS="' + nn + item.ext + '"';
		cmd.RunCommand(cmdLine);
	}

}


function ZeroPad(s, n)
{
	s = s + "";
	while(s.length < n)
	{
		s = "0" + s;
	}
	return s;
}

If you only want it to run on the first selected file, you can tidy things up a bit as there's no need for the Enumerator:

// 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;
    cmd.ClearFiles();

    // Change to "< 1" if you don't mind extra files being selected
    // But still only want the first one to be processed.
    // (To process all files would need an Enumerator in a loop. See Lxp's script.)
    if (tab.selected_files.count != 1)
        return;

    var item = tab.selected_files(0);
    var stem = item.name_stem;
    var ext = item.ext;

    var re = /^(.*-)(\d\d\d\d)$/
    var m = stem.match(re);

    if (!m)
        return;

    var mainParts = m[1];
    var lastPart = parseInt(m[2],10) + 1;
    lastPart = ZeroPad(lastPart,4);

    var nn = mainParts + lastPart + ext;

    var cmdLine = 'Copy DUPLICATE FILE="' + item + '" AS="' + nn + '"';
    cmd.RunCommand(cmdLine);
}


function ZeroPad(s, n)
{
    s = s + "";
    while(s.length < n)
    {
        s = "0" + s;
    }
    return s;
}
1 Like

Million thanks.

1 Like