Page 1 of 1

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

Posted: 23 Feb 2022 13:54
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.

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

Posted: 23 Feb 2022 14:28
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; } }
    }

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

Posted: 23 Feb 2022 15:34
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.

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

Posted: 23 Feb 2022 15:36
by highend
Not really. I'm scripting during sex. Much more enjoyable :ninja:

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

Posted: 23 Feb 2022 15:56
by admin
Ah, the hedonistic coder, brilliant.

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

Posted: 23 Feb 2022 20:27
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.

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

Posted: 23 Feb 2022 21:31
by highend
Post your Teracopy setup in XYplorer?
Post a screenshot of that error message?

XYplorer version?
Teracopy version?

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

Posted: 23 Feb 2022 22:00
by unabatedshagie
2022-02-23_20-54-16.png
2022-02-23_20-59-00.png
Xyplorer version: 22.90
Teracopy version: 3.9

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

Posted: 23 Feb 2022 22:21
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; }

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

Posted: 23 Feb 2022 23:18
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.