Move files by extension type

Discuss and share scripts and script files...
Post Reply
Luigi
Posts: 56
Joined: 01 Sep 2011 00:41

Move files by extension type

Post by Luigi »

Hello, I want to select files in a folder (not all files in that folder) and move them based on their file extension.

For example, I select these files:

Code: Select all

Random File 1.csv
Random File 2.jpg
Random File 3.mp4
Random File 4.html
When I run the script, the script will move these files to their respected folders based on their extensions:
  • Images to "Images"
  • Videos to "Videos"
  • And the rest to "Others"
So in the end they would be in:

Code: Select all

Others \ Random File 1.csv
Images \ Random File 2.jpg
Videos \ Random File 3.mp4
Others \ Random File 4.html
And I want to / may add other categories in the future like "Documents" or "Adobe Files" etc.

I found a similar topic but when I run this script, it opens a menu with 3 items, and if I select a HTML file and click "Move video files" and it moves that HTML file to Video folder. I want to move all of the selected files to their respected folders in one action without opening a menu.

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

Re: Move files by extension type

Post by highend »

Code: Select all

    $exts = <<<>>>
Images=jpg,png
Videos=flv,mp4,avi
>>>;
    $alt = "Other";

    foreach($item, <get SelectedItemsNames <crlf>>, <crlf>, "e") {
        if (exists("<curpath>\$item") == 2) { continue; }
        $ext = gpc($item, "ext");
        $match = regexmatches($exts, "^.+?(=$ext|,$ext)(,|$)");
        if ($match) {
            $dst = gettoken($match, 1, "=", "t");
            moveto "<curpath>\$dst", "<curpath>\$item", , 2, 2;
        } else {
            moveto "<curpath>\$alt", "<curpath>\$item", , 2, 2;
        }
    }
One of my scripts helped you out? Please donate via Paypal

Luigi
Posts: 56
Joined: 01 Sep 2011 00:41

Re: Move files by extension type

Post by Luigi »

Thank you for your help @highend, I really appreciate it. :tup: :tup: :tup:

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

Re: Move files by extension type

Post by highend »

patch syntax, full script, em, what?
One of my scripts helped you out? Please donate via Paypal

Post Reply