Making a dated backup of a folder.

I am a complete newb to applescript, and started learning it yesterday to restart my server in a game.
But I would like to make a script that would take a file named “world” and move it to a folder named “Backups” with a timestamp on it, going like Mth/day/yr, but I don’t know how to have finder move/copy/rename anything.
Help would be much appreciated.

Model: iMac
AppleScript: 2.1.2
Browser: Safari 533.17.8
Operating System: Mac OS X (10.6)

Hi. When I first started I did some tutorials on this website. You can find a list of them here. I did the ones titled “AppleScript Tutorial for Beginners”. They’re short and simple.

To get you started here’s the script you requested. Good luck. :slight_smile:

set thefile to choose file with prompt "Which file do you want to copy?"
set destinationFolder to choose folder with prompt "Where do you want to copy it?"

-- get the date stamp
set dateToday to current date
set dateStamp to ((month of dateToday) as text) & "_" & day of dateToday & "_" & year of dateToday

tell application "Finder"
	-- calculate the new name
	set theName to name of thefile
	set theExtension to name extension of thefile
	set newName to text 1 thru ((count of theName) - (count of theExtension) - 1) of theName & space & dateStamp & "." & theExtension
	
	-- copy the file to destination folder
	duplicate thefile to destinationFolder
	
	-- rename to copied file
	set name of file ((destinationFolder as text) & theName) to newName
end tell

Thanks for the script, wondering if there’s a way to make it prompt once and remember it. I need it to repeat it every 30 minutes.

That’s easy too. Save the following script as an application, and in the “save” window check the box to make it a “stay open” application. Now when you run the applescript application it will initially ask you for the parameters in the “on run” handler. Since the application will “stay open” the “on idle” handler will get run repeatedly every 1800 seconds (30 minutes).

NOTE: we had to change a couple things about the script. First, we had to add the time to the name of the file. Without the time the name of the file would be the same every time we copied it because the month, day, and year didn’t change. So we needed a time to make sure we had a new file name each time. Second, I changed how the file was copied and used the shell command “cp” instead of the Finder. I did this because the Finder has a name length limit and the new file name with the date string added became too long and wasn’t working. The “cp” command didn’t have this limitation.

global theFile, destinationFolder, theExtension, baseName

on run
	set theFile to choose file with prompt "Which file do you want to copy?"
	set destinationFolder to choose folder with prompt "Where do you want to copy it?"
	
	tell application "Finder"
		set theName to name of theFile
		set theExtension to name extension of theFile
	end tell
	
	if theExtension is missing value then
		set baseName to theName
	else
		set baseName to text 1 thru ((count of theName) - (count of theExtension) - 1) of theName
	end if
end run

on idle
	-- calculate the new name
	set newName to baseName & space & my currentDateForFileName() & "." & theExtension
	set newPath to (destinationFolder as text) & newName
	
	-- copy the file and rename it
	do shell script "cp " & quoted form of POSIX path of theFile & space & quoted form of POSIX path of newPath
	
	-- run every 30 minutes
	return 1800
end idle



(*============ SUBROUTINES ==============*)
on currentDateForFileName()
	set now to current date
	set yr to my makeItTwoDigits(year of now)
	set mo to my makeItTwoDigits(month of now as integer)
	set da to my makeItTwoDigits(day of now)
	set hr to my makeItTwoDigits(hours of now)
	set mi to my makeItTwoDigits(minutes of now)
	set se to my makeItTwoDigits(seconds of now)
	return (mo & da & yr & "_" & hr & mi & se)
end currentDateForFileName

on makeItTwoDigits(a)
	set twoDigits to a as Unicode text
	if (count of twoDigits) is less than 2 then
		set twoDigits to "0" & twoDigits
	else if (count of twoDigits) is greater than 2 then
		set twoDigits to text -2 thru -1 of twoDigits
	end if
	return twoDigits
end makeItTwoDigits

That doesn’t work, it says after selecting the files, "cp: folderdirectory is a directory is a folder not a file. Can’t Copy.
And if you didn’t catch in the first post, I’m trying to copy a FOLDER not a file.

Hi. In your first post this is what you said “But I would like to make a script that would take a file named “world” and move it to a folder named “Backups””… so it seemed you wanted to copy a file to a folder. But that doesn’t really matter. There’s simple adjustments to make it do what you want.

Here’s what we need to do. You need to look closely at the scripts I posted and try to understand them. Between the two scripts, the tutorials I mentioned, and maybe a little googling you can write the rest of the script yourself. Give it a try! Post some code and we can discuss it.