[Scripting help] Moving a lot of files based on their names into a specific folder

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
hopeyougetrich
Posts: 2
Joined: 10 Mar 2024 00:19

[Scripting help] Moving a lot of files based on their names into a specific folder

Post by hopeyougetrich »

Greetings!

I have a bunch of files (25k-ish) that mainly look like these below:
yellow_stellar_2024-03-09_08-23-45
lilyth__2024-03-09_08-23-45
rose_2024-03-09_08-23-45

I want to move them into a folder where "_2024-03-09_08-23-45" is removed. So, they will exactly look like this:
"yellow_stellar_2024-03-09_08-23-45" will be moved to "D:\dir\yellow_stellar\"
"_lilyth__2024-03-09_08-23-45" will be moved to "D:\dir\lilyth_\"
"rose_2024-03-09_08-23-45" will be moved to "D:\dir\rose\"
I've been looking and tried a couple of script in this forum, but since I'm no expert in this things, I can't make it work.
I do realize it is kind of tricky because many of them got an extra "_" at the end of their names, and that is why I'm here...

Any help would be appreciated, Thanks! :) :)

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

Re: [Scripting help] Moving a lot of files based on their names into a specific folder

Post by highend »

Select all files to be moved

Code: Select all

    $root = "D:\dir";

    setting "BackgroundFileOps", 0;
    $root  = trim($root, "\", "R");
    $files = <get SelectedItemsPathNames>;
    foreach($file, $files, <crlf>, "e") {
        $name   = gpc($file, "file");
        $prefix = regexmatches($name, "^.+?(?=\d)");
        if (substr($prefix, -1, 1) != "_") { continue; }

        $prefix = substr($prefix, 0, strlen($prefix) - 1);
        moveto $root . "\" . $prefix, $file, , 2, 2, 4, 1, 0, 0, 1, 0, 0;
    }
    #485;
One of my scripts helped you out? Please donate via Paypal

hopeyougetrich
Posts: 2
Joined: 10 Mar 2024 00:19

Re: [Scripting help] Moving a lot of files based on their names into a specific folder

Post by hopeyougetrich »

highend wrote: 10 Mar 2024 03:20 Select all files to be moved

Code: Select all

    $root = "D:\dir";

    setting "BackgroundFileOps", 0;
    $root  = trim($root, "\", "R");
    $files = <get SelectedItemsPathNames>;
    foreach($file, $files, <crlf>, "e") {
        $name   = gpc($file, "file");
        $prefix = regexmatches($name, "^.+?(?=\d)");
        if (substr($prefix, -1, 1) != "_") { continue; }

        $prefix = substr($prefix, 0, strlen($prefix) - 1);
        moveto $root . "\" . $prefix, $file, , 2, 2, 4, 1, 0, 0, 1, 0, 0;
    }
    #485;
This is perfect!
Thankyou for the code and your time sir!

Post Reply