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.
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;
}
@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.
@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.
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;
}