Little question on Applescript

Discussion in 'Mac Programming' started by Colty, Nov 21, 2012.

  1. macrumors newbie

    Joined:
    Nov 21, 2012
    #1
    Hi all,
    I would like to know if it's possible to make an applescript that checks every day a folder on desktop that contains files and folders (in particular folders with a name and a date like "Builds 20 nov 2012", "builds 21 nov 2012" etc etc with files inside) and copy the files inside last folder named "builds 21 nov 2012" (as for today, tomorrow would be "builds 22 nov 2012") depending on the date to a Folder named "last builds" on a nas server that is connected and mounted on desktop, deleting the old files on it, leaving only the last ones.
    If it is possible any help would be appreciated!
    Thanks,
    Al
     
  2. macrumors 65816

    dasx

    Joined:
    Jun 18, 2012
    Location:
    Barcelona
    #2
    Let's see if I understood correctly what you want.

    1. You have a folder in Desktop called -let's say- A/.

    2. You then have:
    A/21.11.2012/
    A/20.11.2012/
    A/19.11.2012/
    […] (more older folders)

    3. You want the script to copy the files in A/21.11.2012/ (last folder) to a folder in a NAS drive, let's call it /Volumes/NAS/LastFiles/.

    Correct?

    There's probably a better way but this is what I'd do. Just create a script with the following commands:

    Code:
    y=`date '+%Y'`
    m=`date '+%m'`
    d=`date '+%d'`
    f=$d.$m.$y #today's folder's name
    
    rsync -aEP --delete /Desktop/A/$f/ /Volumes/NAS/LastFiles/
    
    This will delete everything in /Volumes/NAS/LastFiles/ except those files also present in /Desktop/A/21.11.2012/ (assuming that's the last folder) and copy new files present in /Desktop/A/21.11.2012/.

    I have a couple of these scripts for my own use. I ran them every time I want them to make a backup, in case you wanna just run the script and let it do by itself, I'd add the following to the code.

    Code:
    while [ 1 ]
      do
        y=`date '+%Y'`
        m=`date '+%m'`
        d=`date '+%d'`
        f=$d.$m.$y #today's folder's name
    
        rsync -aEP --delete /Desktop/A/$f/ /Volumes/NAS/LastFiles/
        sleep 5184000 #this will put the script to sleep for 24h
      done
    
    I'm pretty sure this'll work. I'm also sure there's a more elegant way to do it but this is the structure I use for my own.

    Note that -in this second scenario- everytime you reboot your computer you'll need to execute the script again.

    Cheers. :)

    P.S: Please someone correct me if there's a mistake in the code or, as I stated, a better way to do it, as I'll be interested myself!


    ------------------------------

    Added:

    If you go the second way, this is what will happen:

    1. You run the script on 22.11.2012 at 15.00.
    2. It'll sync the folder Desktop/A/22.11.2012/ with /Volumes/NAS/LastFiles/
    (Let's assume this takes 24 minutes)
    3. Go to sleep for 24h.
    4. At 15.24 on the next day, it'll sync the folder Desktop/A/23.11.2012/ with /Volumes/NAS/LastFiles/.
    5…
    6…

    This means that on your NAS/LastFiles folder you'll just have the files in your most recent folder in Desktop/A/. I think this is what you wanted, right?
     
  3. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    I would use launchd to run the script every day. Also a folder named 21.11.2012 isn't the same as particular folders with a name and a date like "Builds 20 nov 2012", "builds 21 nov 2012" etc etc.

    Info : Timed Jobs Using launchd and Running a Job Periodically
     
  4. macrumors 65816

    dasx

    Joined:
    Jun 18, 2012
    Location:
    Barcelona
    #4
    I knew there was a better way to run scripts periodically. :D

    As for the folder's name… Doesn't really matter as that can also be printed out…

    Code:
    day=`date '+%d'`
    month=`date '+%b'`
    year=`date '+%Y'`
    echo "Builds $day $month $year"
    
    That gives:

    Code:
    Mini:~ User$ day=`date '+%d'`; month=`date '+%b'`; year=`date '+%Y'`; echo "Builds $day $month $year"
    Builds 23 nov 2012
    Mini:~ User$ 
    
     

Share This Page