Copying and renaming a file, adding creation/modified date

Hello,

I hope, anyone here can help me with a problem I'm having. First of all I'm not somebody who posts a question without searching, reading or trying, but even after reading hundreds of posts and trying many many different approaches I still can't figure out how to do it.

What I'm trying to do is backup one or many files that are selected in the lister. My button/shortcut should

  • create a folder called "! bu" in the current pane, if it doesn't exist yet
  • copy the highlighted file(s) into that folder
  • rename the copied file(s) from "file.ext" to "yyyy.MM.dd, HH.mm.ss file.ext", using the creation or modified date. Maybe it's best to rename while copying, but I read that only the current date could be used with COPY
  • (Bonus) play a sound if the file hast been backuped correctly, and another sound if it failed
  • (Bonus) Before copying, open the "! bu"-folder in the other pane and scroll down, so that one can see the newly created files

I initially wrote a batch-file to do this. But it is not always working, depending on filenames. So I would like to try within DOpus.

So far I tried:

copy HERE CREATEFOLDER="! bu" WHENEXISTS=skip

copy HERE CREATEFOLDER="! bu" WHENEXISTS=skip AS "{date|yyyy.MM.dd}, {time|HH.mm.ss} .*"

copy HERE CREATEFOLDER="! bu" WHENEXISTS=skip AS "{createddate|yyyy.MM.dd}, {time|HH.mm.ss} .*"


---


copy HERE CREATEFOLDER="! bu" WHENEXISTS=skip
rename PATTERN * TO "{modified|D#yyyy.MM.dd, T#HH.mm.ss} *"


CREATEFOLDER "! bu"
COPY AS *.temp WHENEXISTS=skip
rename PATTERN * TO "{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip


CREATEFOLDER "! bu"
COPY AS *.temp WHENEXISTS=skip
RENAME FROM "! bu\{file}" PATTERN "*" TO "{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip


CREATEFOLDER "! bu"
COPY AS *.temp WHENEXISTS=skip
RENAME FROM "! bu\{file}" PATTERN "*" TO "! bu\{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip


CREATEFOLDER "! bu"
COPY HERE AS *.temp WHENEXISTS=skip
RENAME PATTERN * TO "{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip

CREATEFOLDER "! bu"
COPY HERE AS "! bu\temp_*" WHENEXISTS=skip
rename PATTERN "temp_*" TO "! bu\{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip
Rename REGEXP PATTERN=".....(.+)" to="\1"


CREATEFOLDER "! bu"
COPY {o} to "! bu"
RENAME FROM "{filepath$}\! bu\{file$}" PATTERN "*" TO "{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip

Please don't laugh, I'm not a programmer, I really tried my best :cry:

Could anybody help me here? Thanks in advance,
Nina

You were close :slight_smile:

Try

CreateFolder NAME="! bu" READAUTO=dual,nofocus
Rename PATTERN=* TO="! bu\{modified|D#yyyy.MM.dd, T#HH.mm.ss} *" WHENEXISTS=skip IGNOREEXT

Hello @lxp ,

wow, thank you for a super quick reply...

I tried with your script, but that moves - not copies - the file to the "! bu"-folder. Is there a way to keep the original file in the first folder?

Thanks
Nina

Oops. I knew I should have asked :wink:

That copy-and-rename is indeed a bit trickier.

Try

// https://resource.dopus.com/t/copying-and-renaming-a-file-adding-creation-modified-date/47181

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

    cmd.deselect = false;

    var dest = tab.path + '\\! bu';

    cmd.RunCommand('CreateFolder NAME="' + dest + '" READAUTO=dual,nofocus');
    cmd.SetDest(dest);

    for (var e = new Enumerator(tab.selected); !e.atEnd(); e.moveNext()) {
        var item = e.item();

        var cmdLine = 'Copy' +
            ' FILE="' + item + '"' +
            ' AS="' + item.modify.Format('D#yyyy.MM.dd, T#HH.mm.ss') + ' ' + item.name + '"' +
            ' WHENEXISTS=skip';

        // DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);
    }
}
Button as XML
<?xml version="1.0"?>
<button backcol="none" display="label" hotkey_label="yes" label_pos="right" textcol="none">
	<label>47181</label>
	<tip>copying-and-renaming-a-file-adding-creation-modified-date</tip>
	<icon1>#script</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/copying-and-renaming-a-file-adding-creation-modified-date/47181</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 dest = tab.path + &apos;\\! bu&apos;;</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;CreateFolder NAME=&quot;&apos; + dest + &apos;&quot; READAUTO=dual,nofocus&apos;);</instruction>
		<instruction>    cmd.SetDest(dest);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(tab.selected); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction />
		<instruction>        var cmdLine = &apos;Copy&apos; +</instruction>
		<instruction>            &apos; FILE=&quot;&apos; + item + &apos;&quot;&apos; +</instruction>
		<instruction>            &apos; AS=&quot;&apos; + item.modify.Format(&apos;D#yyyy.MM.dd, T#HH.mm.ss&apos;) + &apos; &apos; + item.name + &apos;&quot;&apos; +</instruction>
		<instruction>            &apos; WHENEXISTS=skip&apos;;</instruction>
		<instruction />
		<instruction>        // DOpus.Output(cmdLine);</instruction>
		<instruction>        cmd.RunCommand(cmdLine);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>

How to use buttons and scripts from this forum

2 Likes

WOW!!! It works! And you managed to do this in just a few minutes. If I told you, how long it took me to create what I posted above, you probably wouldn't believe me...

Thank you so much! I will play around with it and report back after the "field test".

Can't say this enough, thank you thank you thank you,
Nina

Hello @lxp ,

I feel a bit silly having to ask you again, but I was trying to change your script and I'm really struggling to get it to work. I am not someone to cry for help the second I have a problem, I read through countless posts and tried many different things, to no success. If I told you the amount of time I spent on this, you probably wouldn't believe me. As I mentioned before, I'm not a programmer, so again, please don't laugh about my failed attempts you're about to read : (

I was trying to add 2 little changes:

  • I wanted the path of the backup-location to be opened in a new tab (on the other side), instead of in the current tab (on the other side). However, I could only get it to work either opening the tab on the other side but in the background, or opening it but not jumping to the newly created file. I tried all possible combinations of READAUTO in line 11 from above.

  • Then I figured that I would like to have a sound - 3 different ones actually - after the backup. One if the backup-file already exists, one after it has been successfully copied, and one if the backup-file has not been copied successfully. So I stumbled through the forum for another eternity and found @ifexists and Play QUIET. After many different versions here is what I ended up with - but it's still not working:

        var cmdLine = '@ifexists:' +
            'dest' + '\\' + item.modify.Format('D#yyyy.MM.dd, T#HH.mm.ss') + ' ' + item.name + '"' +
            ' Play QUIET "C:\backup_exists.wav" @ifexists:else Play QUIET "C:\backup_exists.wav"';

I inserted this in line 17. But since this would have only taken care of 1 of the 3 sounds, and wasn't even working, and it already took me forever, I decided to come back here and ask you for help. It would be great if you could point me in the right direction.

Thanks in advance
Nina

What's the difference between "backup-file already exists" and "backup-file has not been copied successfully"?

Hello @lxp ,

  • "backup-file already exists" would mean, that the file highlighted has already been copied into the backup-folder before, or already exists there. So if I did a backup of a file before, and didn't change the original in the meantime, and was trying to backup it again. Sort of, what WHENEXISTS=skip checks for, if that makes sense

  • "backup-file has not been copied successfully" would mean that I'm trying to backup the file, but somehow the backup-copy is not in the backup-folder. Basically:

    • 1: Copy "file.ext" to "! bu\2024.01.23, 14.16.49, file.ext"
    • 2: Play a sound if "! bu\2024.01.23, 14.16.49, file.ext" does not exist

    Like "verifying" that the file has been copied.

Thanks : )

Alright. You have a charming way of picking the tricky stuff :wink:

// https://resource.dopus.com/t/copying-and-renaming-a-file-adding-creation-modified-date/47181

// 2024-01-23

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

    cmd.deselect = false;

    var srcFiles = DOpus.Create().Vector(tab.selected);
    var dstFiles = DOpus.Create().Vector();

    var dest = tab.path + '\\! bu';
    cmd.RunCommand('CreateFolder NAME="' + dest + '"');
    cmd.SetDest(dest);

    for (var e = new Enumerator(srcFiles); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        var bakItem = fsu.GetItem(dest + '\\' + item.modify.Format('D#yyyy.MM.dd, T#HH.mm.ss') + ' ' + item.name);
        dstFiles.push_back(bakItem);

        if (fsu.Exists(bakItem)) {
            // backup-file already exists
            cmd.RunCommand('Play FILE="/windows\\Media\\Windows Error.wav" QUIET');
            continue;
        }

        cmd.RunCommand('Copy' +
            ' FILE="' + item + '"' +
            ' AS="' + bakItem.name + '"');

        if (fsu.Exists(bakItem)) {
            // backup-file has been copied successfully
            cmd.RunCommand('Play FILE="/windows\\Media\\Windows Default.wav" QUIET');
        } else {
            // backup-file has not been copied successfully
            cmd.RunCommand('Play FILE="/windows\\Media\\Windows Hardware Fail.wav" QUIET');
        }
    }

    cmd.RunCommand('Go' +
        ' PATH="' + dest + '"' +
        ' NEWTAB=findexisting' +
        ' OPENINDUAL');

    cmd.SetSourceTab(cmd.results.newtabs.count ? cmd.results.newtabs(0) : cmd.desttab);
    cmd.SetFiles(dstFiles);
    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');

    cmd.SetSourceTab(tab);
    cmd.SetFiles(srcFiles);
    cmd.RunCommand('Select NONE');
    cmd.RunCommand('Select FROMSCRIPT SETFOCUS');
    cmd.RunCommand('Select SHOWFOCUS');
}
XML
<?xml version="1.0"?>
<button backcol="none" display="label" hotkey_label="yes" label_pos="right" textcol="none">
	<label>47181b</label>
	<tip>copying-and-renaming-a-file-adding-creation-modified-date</tip>
	<icon1>#script</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>// https://resource.dopus.com/t/copying-and-renaming-a-file-adding-creation-modified-date/47181</instruction>
		<instruction />
		<instruction>// 2024-01-23</instruction>
		<instruction />
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    var cmd = clickData.func.command;</instruction>
		<instruction>    var tab = clickData.func.sourcetab;</instruction>
		<instruction>    var fsu = DOpus.FSUtil();</instruction>
		<instruction />
		<instruction>    cmd.deselect = false;</instruction>
		<instruction />
		<instruction>    var srcFiles = DOpus.Create().Vector(tab.selected);</instruction>
		<instruction>    var dstFiles = DOpus.Create().Vector();</instruction>
		<instruction />
		<instruction>    var dest = tab.path + &apos;\\! bu&apos;;</instruction>
		<instruction>    cmd.RunCommand(&apos;CreateFolder NAME=&quot;&apos; + dest + &apos;&quot;&apos;);</instruction>
		<instruction>    cmd.SetDest(dest);</instruction>
		<instruction />
		<instruction>    for (var e = new Enumerator(srcFiles); !e.atEnd(); e.moveNext()) {</instruction>
		<instruction>        var item = e.item();</instruction>
		<instruction>        var bakItem = fsu.GetItem(dest + &apos;\\&apos; + item.modify.Format(&apos;D#yyyy.MM.dd, T#HH.mm.ss&apos;) + &apos; &apos; + item.name);</instruction>
		<instruction>        dstFiles.push_back(bakItem);</instruction>
		<instruction />
		<instruction>        if (fsu.Exists(bakItem)) {</instruction>
		<instruction>            // backup-file already exists</instruction>
		<instruction>            cmd.RunCommand(&apos;Play FILE=&quot;/windows\\Media\\Windows Error.wav&quot; QUIET&apos;);</instruction>
		<instruction>            continue;</instruction>
		<instruction>        }</instruction>
		<instruction />
		<instruction>        cmd.RunCommand(&apos;Copy&apos; +</instruction>
		<instruction>            &apos; FILE=&quot;&apos; + item + &apos;&quot;&apos; +</instruction>
		<instruction>            &apos; AS=&quot;&apos; + bakItem.name + &apos;&quot;&apos;);</instruction>
		<instruction />
		<instruction>        if (fsu.Exists(bakItem)) {</instruction>
		<instruction>            // backup-file has been copied successfully</instruction>
		<instruction>            cmd.RunCommand(&apos;Play FILE=&quot;/windows\\Media\\Windows Default.wav&quot; QUIET&apos;);</instruction>
		<instruction>        } else {</instruction>
		<instruction>            // backup-file has not been copied successfully</instruction>
		<instruction>            cmd.RunCommand(&apos;Play FILE=&quot;/windows\\Media\\Windows Hardware Fail.wav&quot; QUIET&apos;);</instruction>
		<instruction>        }</instruction>
		<instruction>    }</instruction>
		<instruction />
		<instruction>    cmd.RunCommand(&apos;Go&apos; +</instruction>
		<instruction>        &apos; PATH=&quot;&apos; + dest + &apos;&quot;&apos; +</instruction>
		<instruction>        &apos; NEWTAB=findexisting&apos; +</instruction>
		<instruction>        &apos; OPENINDUAL&apos;);</instruction>
		<instruction />
		<instruction>    cmd.SetSourceTab(cmd.results.newtabs.count ? cmd.results.newtabs(0) : cmd.desttab);</instruction>
		<instruction>    cmd.SetFiles(dstFiles);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT SETFOCUS&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select SHOWFOCUS&apos;);</instruction>
		<instruction />
		<instruction>    cmd.SetSourceTab(tab);</instruction>
		<instruction>    cmd.SetFiles(srcFiles);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select NONE&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select FROMSCRIPT SETFOCUS&apos;);</instruction>
		<instruction>    cmd.RunCommand(&apos;Select SHOWFOCUS&apos;);</instruction>
		<instruction>}</instruction>
	</function>
</button>

Hello @lxp ,

woahhh! I would have never... Well, but I guess, you already know that. Looking at my go above :grin:

I got really sick over night and will not be able to test it out, got a fever, the shivers and am pretty dizzy. I will get back to you once I'm back to normal. I just wanted to shout out a quick ty already!

Ttyl Nina :mask: