Create folder based on filename - first n characters

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
chincherpa
Posts: 37
Joined: 13 Dec 2016 10:58
Location: Stuttgart, Germany

Create folder based on filename - first n characters

Post by chincherpa »

Hi @ all,

I really searched alot, but couldn't find any solution.
Holy shit, this Scripting is powerful and complicated!!

I simply want to move my (selected) JPGs based on their filename to then created folders.

like:
"2016-12-13 12-23-34.jpg" should be moved to the (not yet existing) folder "2016-12"

Name of the folder = first 7 characters of the filename

I really appreciate any hint!

thank you

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

Re: Create folder based on filename - first n characters

Post by highend »

Welcome to the forum

Code: Select all

    foreach($item, <get SelectedItemsPathNames |>) {
        $base   = gpc($item, "base");
        $ext    = gpc($item, "ext");
        $folder = substr($base, 0, 7);
        if (!regexmatches($folder, "[0-9-]{7}") || $ext UnLikeI "jpg") { continue; }
        moveto "<curpath>\$folder", $item, , 2;
    }
One of my scripts helped you out? Please donate via Paypal

chincherpa
Posts: 37
Joined: 13 Dec 2016 10:58
Location: Stuttgart, Germany

Re: Create folder based on filename - first n characters

Post by chincherpa »

As expected, highend has the answer :tup:
But, I thought it's much shorter...

Thank you very much!

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

Re: Create folder based on filename - first n characters

Post by highend »

It can be, I only wanted to show the long version...

Without any validations (.jpg, do the first 7 chars consist of 0-9 + - only...):

Code: Select all

foreach($item, <get SelectedItemsPathNames |>) { moveto substr(gpc($item, "base"), 0, 7), $item, , 2; }
One of my scripts helped you out? Please donate via Paypal

chincherpa
Posts: 37
Joined: 13 Dec 2016 10:58
Location: Stuttgart, Germany

Re: Create folder based on filename - first n characters

Post by chincherpa »

Danke dir!

Post Reply