A small adjustment in Highend's script

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
John_C
Posts: 336
Joined: 16 May 2018 20:04

A small adjustment in Highend's script

Post by John_C »

The script below was created by Highend, based on my very early version. I changed it slightly, but it needs one more small improvement.

It is assumed to be attached to Ctrl-D shortcut. That is, it should replace the default duplicate behavior.

It should create a duplicate of aaa.txt as aaa.txt@2020-01-16-15-32.txt. As you may guess, 15-32 is the current time. In case such file already exist, the duplicate should be aaa.txt@2020-01-16-15-32.txt#2.txt (or #3, #4, etc.)

But it doesn't work now and the problematic block is marked with the corresponding comment. I would be very appreciated for help.

Preliminary setup: Tools > Configuration > Templates > Incremental affix = #2, Date affix = @<date yyyy-mm-dd-hh-nn>

Code: Select all

$affix = getkey('PostfixNum', 'General');
$affixDate = getkey('PostfixDate', 'General');

foreach($item, <get SelectedItemsNames |>,, 'e') {
    // Get the digit(s) from the used affix
    $cntNumAffix = regexmatches($affix, '\d+');

    // How many digits do we have? (e.g. 2 in '01')
    $lenNumAffix = strlen($cntNumAffix);

    // Base file (everything up to but not including the first dot)
    $baseFile = regexmatches(gpc($item, 'file'), '^[^.]+');

    // Keep everything after the first dot
    $exts = regexreplace($item, '^[^.]+\.?');

    // Generate destination file name (without path!)
    if ($exts != '') {
        ; msg 1;
        $dstFile1 = $baseFile . '.' . $exts;
        $dstFile2 = $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.') . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.');
    } else {
        ; msg 2;
        $dstFile1 = $baseFile;
        $dstFile2 = $baseFile . $affix;
    }

    while (true) {
        // If the destination file does NOT exist
        if (exists(<curpath> . '\' . $dstFile1) == False) {
            if ($exts != '') {
                ; msg 3;
                copyas $baseFile . '.' . $exts,, $item;
            } else {
                ; msg 4;
                copyas $baseFile,, $item;
            }
            break;
        }
        if (exists(<curpath> . '\' . $dstFile2) == False) {
            if ($exts != '') {
                ; msg 5;
                // The problem is in the following if-else.
                if ($cntNumAffix == 2) {
                    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
                } else {
                    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.') . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
                }
            } else {
                ; msg 6;
                copyas $baseFile . $affix,, $item;
            }
            break;
        }

        // Destination file DOES already exist
        // Increase the digits of the affix by one
        $cntNumAffix++;

        // Create the new, formatted affix number
        $incAffix = format($cntNumAffix, strrepeat('0', $lenNumAffix));

        // Replace the digits with the incremented one(s)
        $affix = regexreplace($affix, '\d+', $incAffix);

        // This is the new (counted up) destination file name (again, no path!)
        $dstFile2 = $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.') . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
    }
}

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: A small adjustment in Highend's script

Post by highend »

And where is the $affix . $exts part in the first copyas part or how else should a name like this be created?
aaa.txt@2020-01-16-15-32.txt#2.txt
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: A small adjustment in Highend's script

Post by John_C »

The script above isn't very accurate, it's a draft. That is, some code may be missing in some parts thar are not critical right now.

I can make it work, but (haha!) in the not proper way.

Change

Code: Select all

// The problem is in the following if-else.
if ($cntNumAffix == 2) {
    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
} else {
    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.') . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
}
to

Code: Select all

// The problem is in the following if-else.
if ($cntNumAffix == 2) {
    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.') . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
} else {
    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
}
* The first copy will be aaa.txt@2020-01-16-21-10.txt#2.txt.
* The second copy will be aaa.txt@2020-01-16-21-10.txt.
* But actually the order should be the reverse. And I don't know how.

Another option is to change it to

Code: Select all

// The problem is in the following if-else.
if ($cntNumAffix >= 2) {
    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.') . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
} else {
    copyas $baseFile . '.' . $exts . $affixDate . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.'),, $item;
}
The copies will be

* aaa.txt@2020-01-16-21-10.txt#2.txt
* aaa.txt@2020-01-16-21-10.txt#3.txt
* aaa.txt@2020-01-16-21-10.txt#4.txt
* and so on. Here, the problem is that first copy should have no number.

Here is what I'm trying to achieve:

* aaa.txt@2020-01-16-21-10.txt
* aaa.txt@2020-01-16-21-10.txt#2.txt
* aaa.txt@2020-01-16-21-10.txt#3.txt
* aaa.txt@2020-01-16-21-10.txt#4.txt
* and so on.

Of course, if you start the script one minute later, the increment will be reset to the initial state:

* aaa.txt@2020-01-16-21-11.txt
* aaa.txt@2020-01-16-21-11.txt#2.txt
* aaa.txt@2020-01-16-21-11.txt#3.txt
* aaa.txt@2020-01-16-21-11.txt#4.txt
* and so on.

highend
Posts: 13274
Joined: 06 Feb 2011 00:33

Re: A small adjustment in Highend's script

Post by highend »

Code: Select all

            if (exists(<curpath> . '\' . $dstFile2) == False) {
                if ($exts != '') {
                    ; msg 5;
                    // Check if the "pre" version (the one before the e.g. #2 suffix) exists
                    $preDstFile2 = $baseFile . '.' . $exts . $affixDate . '.' . gpc($item, "ext");

                    if (exists(<curpath> . '\' . $preDstFile2) == False) {
                        copyas $preDstFile2,, $item;
                    } else {
                        copyas $dstFile2,, $item;
                    }

                } else {
                    ; msg 6;
                    copyas $baseFile . $affix,, $item;
                }
                break;
            }
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: A small adjustment in Highend's script

Post by John_C »

Yes, thanks a lot, it now works :o

Post Reply