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