File: copy, trim and add current date

Hello,

I am looking for a way to create a button that duplicates a file and then renames that file by replacing the year, month, and day parts.

So when this is the original file: Testfile - dd201005.txt

I would like to have a button that copies the file and renames the new file to: Testfile - dd211123.txt
The dd is a prefix, and the number is in yymmdd format.

When trying to create this, I first copied the file and then started with the rename window. I managed to trim the right part of the file and add the date code at the end. However, it seems I cannot get it to work all the way because the datestamp does not work. I have created a screencast where I show what I do and where things go wrong. Also, I show how the original preset does work.

Is there anyone who can help me to create a script that does the following:

  1. Duplicate a file (Testfile - dd201005.txt → Testfile - dd201005 - Copy.txt)
  2. Trim the final part of the file name; the date code and "- copy" should be removed (Testfile - dd201005 - Copy.txt → Testfile - dd.txt)
  3. Add the current date in the format yymmdd (Testfile - dd.txt → Testfile - dd211123.txt)

Any help or tips is appreciated!

Regards,
Rob

So the final goal is to have a copy with the six digits at the end of the name replaced with the current date?

Yes, that is what I am aiming for. Do you know how to achieve that with one button?

The Copy command can rename files, but it is not as powerful as the Rename command. So we let a script take care of this. The button will work on all selected files.

// https: //resource.dopus.com/t/file-copy-trim-and-add-current-date/39902

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

    // cmd.RunCommand('Set UTILITY=otherlog');
    // DOpus.ClearOutput();

    var currDate = DOpus.Create().Date().Format('D#yyMMdd');

    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var cmdLine = 'Copy DUPLICATE AS="' + item.name_stem.replace(/\d\d\d\d\d\d$/, currDate) + item.ext + '"';
        // DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);
    }
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="both" label_pos="right" textcol="none">
	<label>39902</label>
	<icon1>#newcommand</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https: //resource.dopus.com/t/file-copy-trim-and-add-current-date/39902</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>    // cmd.RunCommand(&apos;Set UTILITY=otherlog&apos;);</instruction>
		<instruction>    // DOpus.ClearOutput();</instruction>
		<instruction />
		<instruction>    var currDate = DOpus.Create().Date().Format(&apos;D#yyMMdd&apos;);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var cmdLine = &apos;Copy DUPLICATE AS=&quot;&apos; + item.name_stem.replace(/\d\d\d\d\d\d$/, currDate) + item.ext + &apos;&quot;&apos;;</instruction>
		<instruction>        // DOpus.Output(cmdLine);</instruction>
		<instruction>        cmd.RunCommand(cmdLine);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>

Please read, if you haven't already:

2 Likes

Wow, thank you, lxp! This script is really useful to me. I definitively would not have been able to create this.

Kindest regards,
Rob