Page 1 of 1

Copy incrementing already existing suffix

Posted: 03 May 2023 05:58
by thalesv666
Hello!
I'm working on a project scanning thousands of documents as pdfs.

They are automatically named like CompanyName--EmployeeName--001.pdf, and the counter increases automatically (ex:--002.pdf) if I have more than one pdf with the same name on the same batch.
However, often I have the same company/employee on different batches, so i need to copy the CompanyName--EmployeeName--001.pdf (and hundreds of other files in subfolders) to a folder that has a file with the same name.
I tried to configure the suffix counter on XYplorer to match the one I used, but I end up with something like CompanyName--EmployeeName--001--001.pdf, CompanyName--EmployeeName--001--002.pdf.

Is there a way to fix this?

Re: Copy incrementing already existing suffix

Posted: 03 May 2023 09:20
by highend
A real world scenario looks like?

Files in _test_suffix_src (including subfolders)

Code: Select all

D:\_test_suffix_src\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_src\CompanyName--EmployeeName--002.pdf
D:\_test_suffix_src\sub\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_src\sub\CompanyName--EmployeeName--002.pdf
Let's say _test_suffix_dst contains:

Code: Select all

D:\_test_suffix_dst\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_dst\CompanyName--EmployeeName--002.pdf
So you now copy all 4 files from src to dst
you'd want a result like this?

Code: Select all

D:\_test_suffix_dst\CompanyName--EmployeeName--001.pdf (stayed the same)
D:\_test_suffix_dst\CompanyName--EmployeeName--002.pdf (stayed the same)
D:\_test_suffix_dst\CompanyName--EmployeeName--003.pdf (original file: D:\_test_suffix_src\CompanyName--EmployeeName--001.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--004.pdf (original file: D:\_test_suffix_src\CompanyName--EmployeeName--002.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--005.pdf (original file: D:\_test_suffix_src\sub\CompanyName--EmployeeName--001.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--006.pdf (original file: D:\_test_suffix_src\sub\CompanyName--EmployeeName--002.pdf)
?

Re: Copy incrementing already existing suffix

Posted: 13 May 2023 09:42
by admin
Disappearing as a way of saying thank you. :roll:

Re: Copy incrementing already existing suffix

Posted: 29 May 2023 23:17
by thalesv666
admin wrote: 13 May 2023 09:42 Disappearing as a way of saying thank you. :roll:
Heyy, sorry for the late response! I have been really busy these days.
Some personal and professional issues happening rn.
I was evicted from where I lived, so had to move to my home town.
Anyway, I really appreciate the help!

Re: Copy incrementing already existing suffix

Posted: 29 May 2023 23:25
by thalesv666
highend wrote: 03 May 2023 09:20 A real world scenario looks like?

Files in _test_suffix_src (including subfolders)

Code: Select all

D:\_test_suffix_src\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_src\CompanyName--EmployeeName--002.pdf
D:\_test_suffix_src\sub\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_src\sub\CompanyName--EmployeeName--002.pdf
Let's say _test_suffix_dst contains:

Code: Select all

D:\_test_suffix_dst\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_dst\CompanyName--EmployeeName--002.pdf
So you now copy all 4 files from src to dst
you'd want a result like this?

Code: Select all

D:\_test_suffix_dst\CompanyName--EmployeeName--001.pdf (stayed the same)
D:\_test_suffix_dst\CompanyName--EmployeeName--002.pdf (stayed the same)
D:\_test_suffix_dst\CompanyName--EmployeeName--003.pdf (original file: D:\_test_suffix_src\CompanyName--EmployeeName--001.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--004.pdf (original file: D:\_test_suffix_src\CompanyName--EmployeeName--002.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--005.pdf (original file: D:\_test_suffix_src\sub\CompanyName--EmployeeName--001.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--006.pdf (original file: D:\_test_suffix_src\sub\CompanyName--EmployeeName--002.pdf)
?
Heyy! Sorry for the delayed response.

It`s somewhat like this, but not exactly. But the final effect I think would be the same.

_test_suffix_src:

Code: Select all

D:\_test_suffix_src\CompanyName\EmployeeName\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_src\CompanyName\EmployeeName\CompanyName--EmployeeName--002.pdf

_test_suffix_dst contains:

Code: Select all

D:\_test_suffix_dst\CompanyName\EmployeeName\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_dst\CompanyName\EmployeeName\CompanyName--EmployeeName--002.pdf
copying from src to dst:

Code: Select all

D:\_test_suffix_dst\CompanyName\EmployeeName\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_dst\CompanyName\EmployeeName\CompanyName--EmployeeName--002.pdf
D:\_test_suffix_dst\CompanyName\EmployeeName\CompanyName--EmployeeName--003.pdf
D:\_test_suffix_dst\CompanyName\EmployeeName\CompanyName--EmployeeName--004.pdf

I usually copy one or two companies at the same time, hundreds of employees subfolders and files inside. I just need to merge the src with the dst, incrementing the suffix counter.

Re: Copy incrementing already existing suffix

Posted: 30 May 2023 19:37
by admin
thalesv666 wrote: 29 May 2023 23:17
admin wrote: 13 May 2023 09:42 Disappearing as a way of saying thank you. :roll:
Heyy, sorry for the late response! I have been really busy these days.
Some personal and professional issues happening rn.
I was evicted from where I lived, so had to move to my home town.
Anyway, I really appreciate the help!
Okay, sorry for my somewhat hasty comment from the sidelines. And sorry for your fate.

It's not my thread so I'll be quiet now.

Re: Copy incrementing already existing suffix

Posted: 01 Jun 2023 19:41
by highend
- Open both panes
- In the right / lower you'll open your destination folder
- In the left you'll navigate to the root source folder
- If you have files in subdirectories there, use a branch view
Select all files that need to be copied
If your branch view shows folders it doesn't matter if you select them as well, the script ignores them
- Execute the script, do this with test data before (make a backup copy of your destination folder) you use it in production

If your destination folder contains gaps in your suffixes, the script won't fill them, it uses the highest one and proceeds with that!
dst folder:

Code: Select all

D:\_test_suffix_dst\CompanyName--EmployeeName--002.pdf
D:\_test_suffix_dst\CompanyName--EmployeeName--005.pdf
and you copy

Code: Select all

D:\_test_suffix_src\CompanyName--EmployeeName--001.pdf
D:\_test_suffix_src\CompanyName--EmployeeName--002.pdf
you'll end with:

Code: Select all

D:\_test_suffix_dst\CompanyName--EmployeeName--001.pdf (the copied --001.pdf)
D:\_test_suffix_dst\CompanyName--EmployeeName--002.pdf (already existed in dst)
D:\_test_suffix_dst\CompanyName--EmployeeName--005.pdf (already existed in dst)
D:\_test_suffix_dst\CompanyName--EmployeeName--006.pdf (the copied --005.pdf)

Code: Select all

    // Copy files (requires dual pane mode!) and increment their suffix

    end (!get("#800")), "No dual pane active, aborted!";
    $aItems = <get SelectedItemsPathNamesSlashed>;
    $aFiles = formatlist($aItems, "f", <crlf>, "!*\");
    end (!$aFiles), "No file(s) selected in active pane, aborted!";

    // Files in inactive pane
    $iPath  = get("path", "i");
    $iFiles = listfolder($iPath, , 1+4, <crlf>);

    $toCopy = "";
    foreach($aFile, $aFiles, <crlf>, "e") {
        $file    = gpc($aFile, "file");
        $base    = gpc($aFile, "base");
        $ext     = gpc($aFile, "ext");
        $escFile = regexreplace($file, "([\\^$.+()\[{])", "\$1");
        // File doesn't exist yet
        if (!regexmatches($iFiles, $escFile)) {
            $iFiles .= <crlf> . $file;
            copyitem $aFile, $iPath . "\" . $file;

        // File does exist
        } else {
            // Get source file suffix and the highest suffix of existing files
            $aSuffix    = regexmatches($base, "\d+?$");
            $iSuffix    = gettoken(regexmatches(regexreplace($iFiles, "\.[^.]+?$"), "\d+?$", <crlf>), -1, <crlf>);
            // Create new file name and copy...
            $lenISuffix = strlen($iSuffix);
            $newSuffix  = format($iSuffix + 1, strrepeat("0", $lenISuffix));
            $newFile    = regexreplace($base, "\d+?$", $newSuffix) . "." . $ext;
            $iFiles    .= <crlf> . $newFile;
            copyitem $aFile, $iPath . "\" . $newFile;
        }
    }

Re: Copy incrementing already existing suffix

Posted: 14 Jun 2023 21:24
by thalesv666
Thank you very much! I`m going to try that script today!

Re: Copy incrementing already existing suffix

Posted: 15 Jun 2023 00:27
by RalphM
...and for the "Thank you" at the bottom we needed a full quote of the post just above. :whistle:

Re: Copy incrementing already existing suffix

Posted: 20 Jun 2023 17:47
by thalesv666
Sorry for the late response again, this case only happens sometimes at work, so I had to wait until I had a subject to test.
Looks like the script didn't work. Tested it with yesterday's work, a company with 300 or so employees. I had 7 files named COMPANY NAME--OUTROS---001.pdf to 007.pdf on the destination folder (OUTROS is the result I get when the employee name isn't found by my python script) and 103 files with the same name on the current working folder. I didn't even used the subfolders yet, but after running the script, the destination had 103 files with that name, kept the 7 that were there already and I got 7 windows popups to skip those files or overwrite.

When I originally posted I was looking for a way to modify the original "rename suffix" to detect if the filename already has a suffix, or force it to substitute the last 3 digits in every filename. Is this possible?

RalphM wrote: 15 Jun 2023 00:27 ...and for the "Thank you" at the bottom we needed a full quote of the post just above. :whistle:
Yeah, and I double posted just before that too.
Need work on my forum skills.
I don't usually post anywhere, my english isn't very good. I can often just read and find what I need, but in this case looks like no one have this kind of problem.
Last time I posted somewhere must've been years ago.

Re: Copy incrementing already existing suffix

Posted: 20 Jun 2023 22:41
by highend
When I originally posted I was looking for a way to modify the original "rename suffix" to detect if the filename already has a suffix, or force it to substitute the last 3 digits in every filename. Is this possible?
No, not with any inbuilt option

I've tested the script with:
First pane path: D:\_test_suffix_src
103 files:

Code: Select all

COMPANY NAME--OUTROS---001.pdf
...
COMPANY NAME--OUTROS---103.pdf
All of them selected

Second pane path: D:\_test_suffix_dst
7 files

Code: Select all

COMPANY NAME--OUTROS---001.pdf
...
COMPANY NAME--OUTROS---007.pdf
After the script is done, the D:\_test_suffix_dst
contains 110 files

Code: Select all

COMPANY NAME--OUTROS---001.pdf
...
COMPANY NAME--OUTROS---110.pdf
Don't know what's wrong on your side...