Script to automatically move files to new directory

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
bifjamod
Posts: 9
Joined: 01 Oct 2015 03:06

Script to automatically move files to new directory

Post 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

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

Re: Script to automatically move files to new directory

Post 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;
        }
    }
One of my scripts helped you out? Please donate via Paypal

armsys
Posts: 557
Joined: 10 Mar 2012 12:40
Location: Hong Kong

Re: Script to automatically move files to new directory

Post 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:

bifjamod
Posts: 9
Joined: 01 Oct 2015 03:06

Re: Script to automatically move files to new directory

Post 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.

bifjamod
Posts: 9
Joined: 01 Oct 2015 03:06

Re: Script to automatically move files to new directory

Post 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.

Post Reply