rename duplicated file with name and current date

Hi. I’m trying to write a backup script to backup my preffs to a preff’s folder. So the backup seems to work, but what i really want to happen is to add the date to the filename (so : a, i can when i created the backup and b, it doesnt replace any existing preffs).

This is my script so far:


tell application "Finder"
	set sourcefile to file "system HD:Users:myname:Library:Preferences:Logic:com.apple.logic.pro"
end tell
tell application "Finder"
	set dest to folder "system HD:Users:myname:Documents:logic preff's"
end tell
tell application "Finder"
	duplicate sourcefile to dest	
end tell

Works so far, but can anyone adivse my on how I can get the date added to the file name?

I thought maybe I need to add an integer to the file name if it already exists “ to stop it replacing the original “ and then put a folder action on the logic preff’s folder to add the date to the name. However, I’m having trouble with the code on the last bit.

I would be greatful for any help, thanx.
truth

No worries, I found it. Might be a bit long winded, but hey, it works!

//backup pref’s to my_pref folder//

set theDate to current date
set int to integer
--  get the day number with leading zero
set dd to text -2 thru -1 of ("0" & theDate's day)
--  note that it is faster to just add a zero than to check whether one is required

--  get an abbreviated month name...
set theShortMonthList to {"Jan", "Feb", "Mar", "Apr", "May", ¬
	"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
copy theDate to b
set month of b to January
set mm to theShortMonthList's item (1 + ((theDate - b + 1314864) div 2629728))

--  ...or, if you prefer, a month number with leading zero
copy theDate to b
set month of b to January
set mm to text -2 thru -1 of ("0" & (1 + ((theDate - b + 1314864) div 2629728)))

--  get the year...
set yyyy to year of theDate

--  or its last 2 digits
set yy to text -2 thru -1 of ((year of theDate) as text)
--  the year needs to be coerced to string (text) before we can split it
set totalSeconds to (time of (current date))
set theHour to totalSeconds div hours
-- gets the number of hours 
set theMinutes to (totalSeconds mod hours) div minutes
-- gets the number of minutes after the hour
set theSeconds to totalSeconds mod minutes

tell application "Finder"
	set mydate1 to dd & "," & mm & "," & yy as text
end tell
tell application "Finder"
	set sourcefile to file "system HD:Users:myname:Library:Preferences:Logic:com.apple.logic.pro"
end tell
tell application "Finder"
	set dest to folder "system HD:Users:myname:Documents:logic preff's"
end tell
tell application "Finder"
	set dupe to (duplicate sourcefile to dest)
	
	set name of dupe to (name of sourcefile & "," & mydate1 & "," & "@" & theHour & " , " & theMinutes & ¬
		" , " & theSeconds & " .")
end tell

You’re doing way too much work to get the date. Try something like this:

property sourceFile : alias ((path to preferences folder as text) & "Logic:com.apple.logic.pro")
property destination : alias ((path to documents folder as text) & "logic preff's:")

tell (current date)
	set theDate to ((text -2 through -1 of ("0" & it's day)) & (it's month as integer) & (text -2 through -1 of (it's year as text))) as text
	set theTime to (",@" & it's hour & ", " & it's minutes & ", " & it's seconds) as text
end tell

tell application "Finder"
	launch
	try
		duplicate sourceFile to destination
		set name of result to (name of sourceFile & theDate & theTime & ".plist")
	on error errorMsg number errorNum
		display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
	end try
end tell

This is great. I think it’ll work for what I need also.

Just two questions:

How can I ensure that at any given time there would only be two backups? (get rid of the oldest one each time a new file is created).

Also, will this copy a file that is open? This file is stored on a server, and I’m not sure how to find out if someone has it open already. Which doesn’t really matter unless that will keep it from being copied.

Thanks all

One more question:

I keep getting an error that says "Can’t set name of “Share:conversion.pdf_backUP_1”

I’ve changed some stuff.

I don’t really need the date. Just “Backup” so this should work right? I keep getting the error (from the previous post)

property sourceFile : "Share:2:conversion.pdf"
property destination : "Share:1:conversion.pdf"

tell application "Finder"
	activate
	try
		
		copy sourceFile to destination
		
		set name of result to (name of file sourceFile & "_backUP_1")
	on error errorMsg number errorNum
		display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
	end try
end tell

Ok,

So now I’ve got part of it to work.

I’d just like to add the part about adding “Backup1” and “Backup2” to the end of the two newest versions of the backup file. I only want to keep two versions of the backup.

I plan on using a recurring daily event in iCal to trigger this script.

Can anyone help me?

set fileToBackup to alias "Share:1:conversion:"
set destinationFolder to alias "Share:2:conversion"

tell application "Finder" to duplicate fileToBackup to destinationFolder replacing yes