Yet another backup script, problems with replacing

Hello everyone! I’ve been reading these board for a month or two now, its an extremely helpful place for applescript stuff. Anyway, i’ve been tasked with creating an applescript to backup some accounting files from an OS9 machine to one of our xserve’s. The files im trying to backup are in a folder thats in the root of the drive of the OS9 machine, and in that folder is a lot of other stuff I don’t need to backup. The files I do need to backup are .sit files, so anyway, here’s my script so far. I plan on either running this script with cron or ical, so once its working im going to hardcode the folders, as of now I just have a prompt asking where the folders are at.


tell application "Finder"
	set backupFolder to (choose folder with prompt "What folder do you want to backup") as alias
	set serverFolder to (choose folder with prompt "Where do you want to backup the files to?") as alias
	duplicate (every file of folder backupFolder whose name contains ".zip") to folder serverFolder
end tell

The problem I have is that the script runs once fine, but the next time it tries to duplicate a file that already exists and gives me an error. I’m having a problem understanding replacing I guess. In the applescript dictionary for finder it says “[replacing boolean] : Specifies whether or not to replace items in the destination that have the same name as items being duplicated”, so i’ve tried adding “with replacing false” or “without replacing” at the end of my duplicate line, but neither of them seem to work. Essentially what I want is if the file is already backed up, I just want the script to not copy that file. As a side note, I have it copying .zip files instead of .sit files at the moment for ease of testing.

Thanks in advance for any help that anyone can provide. I’ve tried searching for the answer but replacing comes up in a lot of threads and I haven’t found the answer yet.

P.S.
When I started this, I wanted the script to ask what folder to backup, and where the destination folder is at, similar to how it is now. I then wanted to permanently set those paths, and have my backup script skip the asking of where the folders are at if those paths have been previously set. That way I could set this up for other machines, etc. I was unable to figure this out either, so if anyone has an idea please help =).

Browser: Firefox 2.0
Operating System: Mac OS X (10.4)

Hi Major

I think theres a few ways to capture in a script what propblem your having, the easiest is this
you can put the duplicate part of your script in a try block to capture the problem.


tell application "Finder"
	set backupFolder to (choose folder with prompt "What folder do you want to backup") as alias
	set serverFolder to (choose folder with prompt "Where do you want to backup the files to?") as alias
	try
		duplicate (every file of folder backupFolder whose name contains ".zip") to folder serverFolder
	end try
end tell

You may want to add some code that adds a date or time to the end of the file, for differnet versions etc… i’m sure someone else will add
to this thread with multiple ideas on where you can go with this…
Has for your other question you already have the answer in your script. you just want to place in the result
of what your first 2 lines return. there both paths to the folders you want.
(Alias blah blah blah .etc…)
just run your script once and copy the paths from the events log into your script.

set backupFolder to alias blah blah blah .etc

Hi,

One thing about using the error handler is that it won’t backup anything after the error. You could use a repeat loop and just backup those that don’t error. Here’s another way:


property backupFolder : missing value
property serverFolder : missing value
property firstRun : true
--
if firstRun then
	set backupFolder to choose folder -- target folder
	set serverFolder to choose folder -- destination folder
	set firstRun to false
end if
tell application "Finder"
	set serverItems to name of every item of serverFolder
	set backupItems to every item of backupFolder whose name is not in serverItems
	duplicate backupItems to serverFolder
end tell

I didn’t use the extension filter for testing. Using the flag method has its downside and there are several other methods. I think the best method is to place the folders in folders that are always there. So, you would ask the user for the containers for the folders and check if the target and destination folders exist. If they don’t exist, then make new ones.

gl,

Here’s another one that checks for existance.


property backupFolder : missing value
property serverFolder : missing value
--
tell application "Finder"
	if not (exists backupFolder) then
		set backupFolder to choose folder
	end if
	if not (exists serverFolder) then
		set serverFolder to choose folder
	end if
	set serverItems to name of every item of serverFolder
	duplicate (every item of backupFolder whose ¬
		name is not in serverItems) to serverFolder
end tell

Thanks for the replies!

The data files that im backing up already are timestamped by the accounting program that creates the backups, so that part is taken care of.

I will try these suggestions out to see if they get me anywhere, thanks again!

Josh

P.S.
Kel, I think the script you provided is exactly what I was looking for, except when I try to run it, when it gets to the duplicate command I get an error, “Finder got an error: The operation could not be completed”. If this helps im working on a Tiger machine, and was just using a folder on my desktop called backup_test, in that folder is a couple zip files and a folder called svr. I chose the backup_test folder for backup, and svr for the server folder, after I chose that second folder it gives me that error.

I will try to toy around with it, you’ve definitely pointed me into the right direction, thanks again!

What did the error say MM?

If you insist on “zip” as the extension add it to the duplicate line of kel’s script:


property backupFolder : missing value
property serverFolder : missing value
--
tell application "Finder"
	if not (exists backupFolder) then
		set backupFolder to choose folder
	end if
	if not (exists serverFolder) then
		set serverFolder to choose folder
	end if
	set serverItems to name of every item of serverFolder
	duplicate (every item of backupFolder whose ¬
		name is not in serverItems and name extension is "zip") to serverFolder
end tell

If you want to know a lot about each file, this prepares three list for every file, even those in folders. (Item 3 of each for example, contains the data for the third file):

tell application "Finder" to {N:name, md:modification date, ne:name extension} of (files of entire contents of (choose folder))

Edit:
I added the part where it only copies zip files to my duplicate line and it appears to be working just fine now. Maybe I had a typo or something im not sure, but everything worked.

Thanks again!


Old post:
The error I was getting is:

“Finder got an error: The operation could not be completed”.

This happens on the duplicate line. Sorry, the error on my last post was kinda buried in a paragraph.

Right now im trying different things to see where the error is at, the error I get isn’t very helpful. So far I can tell that serverItems is giving me a list of whats in the right folder, so trying to see what the other variables end up being, etc.

Thanks again for any help.

Model: Powermac Dual G5
Browser: Firefox 2.0
Operating System: Mac OS X (10.4)