Page 1 of 1

Create folder based on filename - first n characters

Posted: 13 Dec 2016 13:31
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

Re: Create folder based on filename - first n characters

Posted: 13 Dec 2016 14:30
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;
    }

Re: Create folder based on filename - first n characters

Posted: 13 Dec 2016 14:42
by chincherpa
As expected, highend has the answer :tup:
But, I thought it's much shorter...

Thank you very much!

Re: Create folder based on filename - first n characters

Posted: 13 Dec 2016 15:08
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; }

Re: Create folder based on filename - first n characters

Posted: 13 Dec 2016 15:26
by chincherpa
Danke dir!