Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 21, 2012, 02:21 PM   #1
Colty
macrumors newbie
 
Join Date: Nov 2012
Little question on Applescript

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
Colty is offline   0 Reply With Quote
Old Nov 21, 2012, 04:32 PM   #2
dasx
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
Quote:
Originally Posted by Colty View Post
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
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?
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"
dasx is offline   0 Reply With Quote
Old Nov 21, 2012, 05:27 PM   #3
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
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
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Nov 22, 2012, 05:24 PM   #4
dasx
macrumors 65816
 
dasx's Avatar
 
Join Date: Jun 2012
Location: Barcelona
Quote:
Originally Posted by kryten2 View Post
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
I knew there was a better way to run scripts periodically.

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$
__________________
Grey computer which does complicated stuff.
Cell phone that can talk to me. (Seriously).
"Fancy quote from dead guy"
dasx is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 10:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC