Highend, thanks a lot, again. I'm sure that I couldn't do this on my own.
I have made the following changes:
1. When you copy the file to the directory that doesn't have the file with such the name - the file is copied without the affix.
2. Now you can copy files without extensions (e.g. README) and directories.
To make it work for files without extensions and for directories, it was necessary (besides other changes, of course) to change
!= 1 to
== 0 (actually, to
== False, just a matter of personal style, I suppose). I'm wondering for what reason you decided to use
!= instead.
There are two issues that I don't understand how to fix:
1. When you copy the file to the directory that already have the file with the such name - the file is simply copied instead of asking the user.
2. When you copy multiple files, the copy window is created/closed for each file separately.
I have tried to fix the second issue in the following way: a) inside the
foreach loop, instead of copying, create a string, b) perform the copying based on this string after the loop. But I have not succeeded with it and not sure that it possible at all. These attempts are not reflected in the version that I post below.
Of course, I don't expect that you should expend your own time with further improvements. It's a user forum, not a paid coding service.
Code: Select all
$affix = getkey('PostfixNum', 'General');
foreach($item, <clp>, <crlf>, '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 . $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;
copyas $baseFile . '.' . $exts . $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 . $affix . '.' . regexreplace($exts, '^.+(#\d+|@[\d-]+)\.');
}
}
Tested with:
Code: Select all
Increment affix: #2
Date affix: @<date yyyy-mm-dd-hh-nn>