Page 1 of 1

Script to automatically move files to new directory

Posted: 01 Oct 2015 03:15
by bifjamod
I'm recently new to Xyplorer, and have no experience with scripting beyond what I can do with VBA in Excel, and a wee bit of experience with AppleScript. I'm looking to replace some functionality I had with Hazel on the Mac - namely, moving files from my "working" directory to their permanent home. Because Xyplorer has scripting capability, it seems to me I can avoid the necessity of a "middle-man" app ("Febooti Automation Workshop", in this case, where I also cannot figure out a workable script!) and have Xyplorer do the heavy lifting.

Said files are all 5 digits following by 2-n characters, such as 31349BLG.pdf; once I'm done with them, I need them to be moved to C:\Dropbox\Active Files\31000-31999\31300-31399\. Of course, the file number range could be 312nn, 314nn, etc, or even 29nnn.

Can anyone help me out with a starter script, a full-blown script, or a contact that would be willing to write the script for $$?

Thanks

Re: Script to automatically move files to new directory

Posted: 01 Oct 2015 09:50
by highend
Use it on the selected file(s) of your choice. It's only testing if your files begins with 5 (or more) digits, followed by 2-3 (your example contains 3-n, not 2-n chars) characters to process them...

Code: Select all

    foreach($file, "<get SelectedItemsPathNames |>") {
        $fileName = getpathcomponent($file, "file");
        if (regexmatches($fileName, "^\d{5,}[a-z]{2,3}")) {
            $digits = regexmatches($fileName, "^\d+");
            $rangeThousands = substr($digits, 0, 2);
            $rangeHundreds  = substr($digits, 0, 3);
            $rangeThousandsDir = $rangeThousands . "000-" . $rangeThousands . "999";
            $rangeHundredsDir  = $rangeHundreds . "00-" . $rangeHundreds . "99";
            moveto "C:\Dropbox\Active Files\$rangeThousandsDir\$rangeHundredsDir", $file, , 2;
        }
    }

Re: Script to automatically move files to new directory

Posted: 01 Oct 2015 10:14
by armsys
highend wrote:Use it on the selected file(s) of your choice.
Thanks Highend for teaching us how to apply regex in XY scripts. You're my hero. :appl: :cup:

Re: Script to automatically move files to new directory

Posted: 01 Oct 2015 22:26
by bifjamod
highend wrote:Use it on the selected file(s) of your choice. It's only testing if your files begins with 5 (or more) digits, followed by 2-3 (your example contains 3-n, not 2-n chars) characters to process them...
Thank you! I also second the kudos for showing how to use Regex in these scripts; I've only scratched the surface with Regex but see it's value. I'll give this a whirl.

Re: Script to automatically move files to new directory

Posted: 29 Mar 2016 23:34
by bifjamod
highend wrote:Use it on the selected file(s) of your choice. It's only testing if your files begins with 5 (or more) digits, followed by 2-3 (your example contains 3-n, not 2-n chars) characters to process them...
Thank you again! It's only taken me 6 months to get back to this - wish I had done it sooner, as it would have saved me a lot of headaches and wasted time. Works perfectly for my needs.