Copy files to folders with similar names

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
ColdNose
Posts: 36
Joined: 16 Jun 2017 09:21
Location: Win11, 3 monitors: (1) 1920x1200 @ 125%, (2) 3840x2160 @ 175%

Copy files to folders with similar names

Post by ColdNose »

Found a similar request and script from highend in this post: viewtopic.php?f=3&t=16799&p=144622#p144617, but my needs are a little different and I can't figure out how to tweak the script.

I have thousands of files with unique 10 digit prefixes to move into existing folders in the same directory with names that start with the same prefix but have additional characters. If for some reason a folder with the prefix doesn't exist, then one should be created with just the prefix as the name. For example:

TH12345678 - prelim.mp4
TH12345678 - post.mp4
TH12345678 image file.jpg
TH12352333 - prelim.mp4

To move into existing folders:
TH12345678 Production Files
TH12352333 SW Pre-production Files


Also, the script should be run against all the files in the directory as there are too many to select. Any help is appreciated!

highend
Posts: 14662
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Copy files to folders with similar names

Post by highend »

You should make sure that the current folder only contains files with the prefix, otherwise e.g. an arbitrary file like
xtrafile.txt (a file with 10+ chars) would be moved into xtrafile.t...

Code: Select all

    // Get file(s) & folder(s) without path
    $files   = listpane(, , 1+4, <crlf>);
    $folders = listpane(, , 2+4, <crlf>);

    // Loop over all file(s) (and try to create blocks with the same prefix)
    while ($files) {
        // Get prefix
        $first  = gettoken($files, 1, <crlf>);
        $prefix = substr($first, 0, 10);

        // If $prefix == $first, remove it from the $files list without further processing
        if ($prefix == $first) {
            $files = regexreplace($files, "^" . $prefix . ".*?(\r?\n|$)");
            continue;
        }

        // Get all other file(s) with the same prefix
        $matchFiles = regexmatches($files, "^" . $prefix . ".*?(?=\r?\n|$)", <crlf>);

        // Move into which folder?
        $matchFolder = gettoken(regexmatches($folders, "^" . $prefix . ".*?(?=\r?\n|$)", <crlf>), 1, <crlf>);
        if (!$matchFolder) { $matchFolder = $prefix; }

        // Remove them from the $files list
        $files = regexreplace($files, "^" . $prefix . ".*?(\r?\n|$)");

        // Move them
        moveto "<curpath>\$matchFolder", $matchFiles, , 2, 2, 4;
    }
One of my scripts helped you out? Please donate via Paypal

ColdNose
Posts: 36
Joined: 16 Jun 2017 09:21
Location: Win11, 3 monitors: (1) 1920x1200 @ 125%, (2) 3840x2160 @ 175%

Re: Copy files to folders with similar names

Post by ColdNose »

Highend! Thank you very, very much!

It works perfect -- and FAST! I would've never thought of this approach nor figured out the "regexreplace" commands (still haven't :roll: ). This is something I need to learn! I was stuck using "exists" and, as it doesn't recognize wildcards, I couldn't figure out how to search for a partial folder name.

You have saved me hours of work (and drag/drop mistakes) and I will be sending you a few beers via Paypal. I am most grateful. Thank you again.

highend
Posts: 14662
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Copy files to folders with similar names

Post by highend »

No problem and thanks for the beers :mrgreen:

The regexes are rather simple.

"^" . $prefix . ".*?(\r?\n|$)"
^ = Beginning of the line + $prefix + everything after it (if at all) including the line terminator (<crlf>) or the end of the line if there is no terminator

Such a regex is necessary to remove the complete lines so that no empty parts are left to allow the while loop to terminate itself once it's empty

(?=\r?\n|$)
Only difference: Do not include the <crlf> or $ (end of line)

The script is fast because of this technique. E.g. if you have ~3 files that belong into each existing folder and 10.000 files in total the script needs only ~3.334 loops instead of 10.000
One of my scripts helped you out? Please donate via Paypal

xycool
Posts: 6
Joined: 27 Oct 2018 20:02

Re: Copy files to folders with similar names

Post by xycool »

hello, highend
your script works fast and smooth !! :D :D


Can I ask you one more question related? sorry, I'm a terrible newbie.

I have thousands of files in a folder like this :

kenzi taylor - white dizzy 01 to the moon [004].epub
Elizabeth_Markwood - Blue Stars 01 Blues to Skyline (French)(7th Edition).epub
Z Wenner - Star_Search 03 loca_With You (4.14.2014).jpeg
X Yratare - The ampire's 7 (2nd)(2015.0.07).exe
J.B. Kari Woodgreen - Clockwork Staar - Collect.epub

--------After (folder name)------
kenzi taylor
Elizabeth Markwood
Z Wenner
X Yratare
J B Kari Woodgreen
----------------------------------------


Could you just have their folders named by the whole letters of the left side to '-' (hyphen)
yet some files have more than two "-" so you could just ignore from the second one.

highend
Posts: 14662
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Copy files to folders with similar names

Post by highend »

You can't do this with a block building routine like in the posted script if the moved files should have their leading name cleaned up as well.
In that case every file must be processed one by one...
One of my scripts helped you out? Please donate via Paypal

Post Reply