Copy incrementing already existing suffix

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
thalesv666
Posts: 5
Joined: 03 May 2023 05:47

Copy incrementing already existing suffix

Post 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?

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

Re: Copy incrementing already existing suffix

Post 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)
?
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Copy incrementing already existing suffix

Post by admin »

Disappearing as a way of saying thank you. :roll:

thalesv666
Posts: 5
Joined: 03 May 2023 05:47

Re: Copy incrementing already existing suffix

Post 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!

thalesv666
Posts: 5
Joined: 03 May 2023 05:47

Re: Copy incrementing already existing suffix

Post 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.

admin
Site Admin
Posts: 60357
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Copy incrementing already existing suffix

Post 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.

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

Re: Copy incrementing already existing suffix

Post 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;
        }
    }
One of my scripts helped you out? Please donate via Paypal

thalesv666
Posts: 5
Joined: 03 May 2023 05:47

Re: Copy incrementing already existing suffix

Post by thalesv666 »

Thank you very much! I`m going to try that script today!
Last edited by thalesv666 on 20 Jun 2023 17:48, edited 1 time in total.

RalphM
Posts: 1932
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Re: Copy incrementing already existing suffix

Post by RalphM »

...and for the "Thank you" at the bottom we needed a full quote of the post just above. :whistle:
Ralph :)
(OS: W11 22H2 Home x64 - XY: Current beta - Office 2019 32-bit - Display: 1920x1080 @ 125%)

thalesv666
Posts: 5
Joined: 03 May 2023 05:47

Re: Copy incrementing already existing suffix

Post 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.

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

Re: Copy incrementing already existing suffix

Post 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...
One of my scripts helped you out? Please donate via Paypal

Post Reply