Sort / Move / File Desktop Files into Folders that have been recently created (within 5 days).

Discuss and share scripts and script files...
Post Reply
leemacread2
Posts: 2
Joined: 05 Dec 2018 23:17

Sort / Move / File Desktop Files into Folders that have been recently created (within 5 days).

Post by leemacread2 » 05 Dec 2018 23:43

Hi, this is my first xyplorer forum post and my first attempt at a xyplorer script - not sure if someone can help me.?

I have tried to write a script that...

a) finds any recent files (created within the last 5 days) on my desktop
b) take these recent files and put them into a specific folder based on their file extension.

Any files older than 5 days remain untouched on my desktop.

sounds easy... (and it probably is to someone).

I have been through various posts and 'cobbled' together the below effort.

(this is a total rip of other peoples hard work and forum answers, just thrown together so apologies and thanks to anyone who unwillingly contributed).

Code: Select all


$x = 10;
 $z = 1;
 while ($z <= $x) {

// loop through the whole list
   sel 1; // go to top first, if you want.
    
   while ( "<curitem>" != "" )
   {

        /*
               See Help > Variables
               <date>      current date
               <datem>     modified date of the current list item
               <datec>     created date of the current list item
               <datea>     accessed date of the current list item
        */

        //do an diff on current item against current date:

        //datediff(date1, [date2=now], [interval=d])

        $DateDiff = datediff("<datec>");

        //check if DateDiff:
        if($DateDiff > 5){

                    //if yes, do nothing

        }else{

                    if ("<curext>"=="pdf")
{
    focus; moveto "C:\Users\Megaport\Desktop\1. PDF files\",,,,,5;focus; 
}
 
                    if ("<curext>"=="dwg")
{
    focus; moveto "C:\Users\Megaport\Desktop\5. Dwg files\",,,,,5; focus; 
}

                    if ("<curext>"=="txt")
{
    focus; moveto "C:\Users\Megaport\Desktop\4. Text files\",,,,,5; focus; 
}

                    if ("<curext>"=="doc")
{
    focus; moveto "C:\Users\Megaport\Desktop\4. Text files\",,,,,5; focus; 
}

                    if ("<curext>"=="docx")
{
    focus; moveto "C:\Users\Megaport\Desktop\4. Text files\",,,,,5; focus;
}

                    if ("<curext>"=="xls")
{
    focus; moveto "C:\Users\Megaport\Desktop\3. Xls files\",,,,,5; focus; 
}

                    if ("<curext>"=="xlsx")
{
    focus; moveto "C:\Users\Megaport\Desktop\3. Xls files\",,,,,5; focus; 
}

                    if ("<curext>"=="jpg")
{
    focus; moveto "C:\Users\Megaport\Desktop\2. Image files\",,,,,5; focus; 
}

                    if ("<curext>"=="png")
{
    focus; moveto "C:\Users\Megaport\Desktop\2. Image files\",,,,,5; focus; 
}
                    if ("<curext>"=="bmp")
{
    focus; moveto "C:\Users\Megaport\Desktop\2. Image files\",,,,,5; focus; 
}

                    if ("<curext>"=="gif")
{
    focus; moveto "C:\Users\Megaport\Desktop\2. Image files\",,,,,5; focus; 
}

                    if ("<curext>"=="zip")
{
    focus; delete 1, 0, "<curitem>"; focus; 
}

                    if ("<curext>"=="rar")
{
    focus; delete 1, 0, "<curitem>"; focus; 
}

                    if ("<curext>"=="msg")
{
    focus; moveto "C:\Users\Megaport\Desktop\4. Text files\",,,,,5; focus;
}

                    if ("<curext>"=="eml")
{
    focus; moveto "C:\Users\Megaport\Desktop\4. Text files\",,,,,5; focus;
}

        }
        
     sel "+1";
    }

   $z++;
 }


This (sort-of) works!

However, its slow.

Its hit and miss when moving the files, lots of times it misses them - so I created a crazy loop, to run it over and over (making it slower), but it still misses stuff.

Its ugly.

Im sure there is a beautiful, elegant, simple script that could do this sort of thing much much faster - I just dont know how to do it.

Any help would be much appreciated.

Thanks in advance for any replies.

highend
Posts: 7656
Joined: 06 Feb 2011 00:33

Re: Sort / Move / File Desktop Files into Folders that have been recently created (within 5 days).

Post by highend » 06 Dec 2018 00:24

- No "visual" selection
- Easier to maintain / extend (more extensions, destination folders)

Code: Select all

    $diff = 5; // Difference in days
    $src  = "%desktopreal%";
    $assignments = <<<>>>
1. PDF files|pdf
2. Image files|jpg;png;bmp;gif
3. Xls files|xls;xlsx
4. Text files|txt;doc;docx;msg;eml
5. Dwg files|dwg
Delete|zip;rar
>>>;

    $find = "";
    foreach($item, $assignments, <crlf>, "e") {
        $exts = trim(gettoken($item, 2, "|"), ";");
        if ($exts) { $find .= "*." . replace($exts, ";", ";*.") . ";"; }
    }
    $find = formatlist($find, "e", ";");
    $files = quicksearch("$find /nf", $src, , "s");

    setting "BackgroundFileOps", 0;
    foreach($file, $files, <crlf>, "e") {
        if (datediff(property("#date.c", $file)) <= $diff) {
            $dst = gettoken(regexmatches($assignments, "^.+\|.*" . gpc($file, "ext")), 1, "|");
            if !($dst) { continue; }
            if ($dst LikeI "Delete") { delete 1, 0, $file; }
            else { moveto "$src\$dst", $file, , 2, 2, 5; }
        }
    }
One of my scripts helped you out? Please donate via Paypal or highend (at) web (dot) de

leemacread2
Posts: 2
Joined: 05 Dec 2018 23:17

Re: Sort / Move / File Desktop Files into Folders that have been recently created (within 5 days).

Post by leemacread2 » 06 Dec 2018 01:33

spot on!

ha ha ... that works a treat, nice one.

and its fast + easily extendable.

I am going to spend the next week (or so) trying to get my head around the workings of it ... I need to learn this magic.

Thanks for the fast reply and great solution.

Post Reply