Looking to create a button that runs a script to move files into a subfolder based on age

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
unabatedshagie
Posts: 33
Joined: 07 Mar 2018 12:10

Looking to create a button that runs a script to move files into a subfolder based on age

Post by unabatedshagie »

I take a lot of screenshots for my work.

I'm looking to have a button that moves all images (or all files I guess) into subfolders based on dates.

Screenshots
    > _older
        > file 1 - 30 days ago
        > file 2 - 21 days ago
    > last 7 days
        > file 3 - 14 days ago
    > file 4.png  - 2 days ago
    > file 5.png  - today


I'm looking for when I click the button the files to get organised like this:

Screenshots
    > _older
        > file 1 - 30 days ago
        > file 2 - 21 days ago
        > file 3 - 14 days ago
    > last 7 days
        > file 4.png  - 2 days ago
    > file 5.png  - today


So basically after clicking the button, the _older folder has anything 8 days or older, last 7 days has anything between 2 and 7 days old and the root screenshots folder only has anything created today.

The script would have to go into the last 7 days folder and move things out of that I guess for this to work.

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

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by highend »

Code: Select all

    $root  = "<curpath>\Screenshots";
    $older = "<curpath>\Screenshots\_older";
    $seven = "<curpath>\Screenshots\last 7 days";

    // m = name|size|modified|created|accessed|attributes
    $images = quicksearch("/types={:Image}", $root, , "sm");
    foreach($image, $images, <crlf>, "e") {
        $item    = gettoken($image, 1, "|");
        $file    = gpc($item, "file");
        $created = gettoken($image, 4, "|");
        $diff    = datediff($created); // In days by default
        if ($diff >= 8)     { if (exists("$older\$file") != 1) { moveto $older, $item, , , 2, , 1; } }
        elseif ($diff >= 2) { if (exists("$seven\$file") != 1) { moveto $seven, $item, , , 2, , 1; } }
        else                { if (exists("$root\$file")  != 1) { moveto $root,  $item, , , 2, , 1; } }
    }
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by admin »

Your scripting speed and accuracy is impressive. I'm assuming you're doing the dishes and watching a movie while you're at it.

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

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by highend »

Not really. I'm scripting during sex. Much more enjoyable :ninja:
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 67299
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by admin »

Ah, the hedonistic coder, brilliant.

unabatedshagie
Posts: 33
Joined: 07 Mar 2018 12:10

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by unabatedshagie »

Thanks for the script.

There is one issue that I'm not sure if it's possible to work around.

I have teracopy set up as my default copy handler, when I run the script I'm getting a message "Teracopy failed. Press OK to try with Custom Copy." When I click OK a file move then I get the same error message again.

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

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by highend »

Post your Teracopy setup in XYplorer?
Post a screenshot of that error message?

XYplorer version?
Teracopy version?
One of my scripts helped you out? Please donate via Paypal

unabatedshagie
Posts: 33
Joined: 07 Mar 2018 12:10

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by unabatedshagie »

2022-02-23_20-54-16.png
2022-02-23_20-59-00.png
Xyplorer version: 22.90
Teracopy version: 3.9
To see the attached files, you need to log into the forum.

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

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by highend »

...|Copy|Move
?

These are operations and not switches?

A different approach...

Tick that setting
Configuration | General | Sort and Rename | Rename | [x] Allow move on rename

Code: Select all

    $root  = "<curpath>\Screenshots";
    $older = "<curpath>\Screenshots\_older";
    $seven = "<curpath>\Screenshots\last 7 days";

    // m = name|size|modified|created|accessed|attributes
    $oldFiles = "";
    $newFiles = "";
    $images   = quicksearch("/types={:Image}", $root, , "sm");
    foreach($image, $images, <crlf>, "e") {
        $item      = gettoken($image, 1, "|");
        $file      = gpc($item, "file");
        $created   = gettoken($image, 4, "|");
        $diff      = datediff($created); // In days by default
        if     ($diff >= 8) { if (exists("$older\$file") != 1) { $oldFiles .= $item . <crlf>; $newFiles .= "$older\$file" . <crlf>; } }
        elseif ($diff >= 2) { if (exists("$seven\$file") != 1) { $oldFiles .= $item . <crlf>; $newFiles .= "$seven\$file" . <crlf>; } }
        else                { if (exists("$root\$file")  != 1) { $oldFiles .= $item . <crlf>; $newFiles .= "$root\$file"  . <crlf>; } }
    }
    if ($newFiles) { rename "l", $newFiles, , $oldFiles; }
One of my scripts helped you out? Please donate via Paypal

unabatedshagie
Posts: 33
Joined: 07 Mar 2018 12:10

Re: Looking to create a button that runs a script to move files into a subfolder based on age

Post by unabatedshagie »

I just found that copy handler from searching the forum, I've been using it for a while with no issues.

Ticking that checkbox and using the new script seems to be working now though. Thanks.

Post Reply