kcw | journal | 2000 << Previous Page | Next Page >>

I thought I'd take up a journal entry or two describing the Delete Sync Files script that I wrote. If you remember, I had a script that ran Simple Backup to backup my files. It would copy any files if the date or size information was different than in the destination, but it didn't check the destination for files that had been deleted in the source, so it didn't delete the files. With that simple addition it would truly do File Synchronization and not just file backups. The File Synchronization Control Panel, even after updating it with Mac OS 9.0.4, still didn't kept crashing when I tried to configure it, so that was still out of the picture.

I finally decided to take the time to write a script to do the file deletion part. Now, I had looked at the Satimage Additions library, included with the AppleScript editor Smile, and it had a backup command. But it did much what Simple Backup did, and didn't delete files in the destination. What it could do though, was just list the files it would have backed up, and in fact it listed the files in two groups: a files to be created group and a files to be changed group.

What I could do is run the backup command, using the Simple Backup destination as the source and the Simple Backup source as the backup destination, as if I were going to do a reverse backup. This would give me a list of files to be created (ignoring the files to be changed, which should be no files if this is done right after Simple Backup runs), which are the files that should be deleted. Delete those files and you'd end up with the Simple Backup destination folder synchronized to the source folder.

Let's take a look at the start of the script:

-- procedure deleteSyncFiles

-- v_source: backup source directory
-- v_source_base_length: length of everything in v_source but last
-- directory name
-- v_destination: backup destination
-- v_destination_base: everything in v_destination but last directory name

-- Delete any files in v_destination that are not in v_source. We need
-- v_source_base_length and v_destination_base because the utility we
-- use returns the new files that would go to v_source. So we have to
-- replace the beginning part of v_source (up to v_source_base_length)
-- with v_destination_base.

on deleteSyncFiles (v_source, v_source_base_length,
v_destination, v_destination_base)

I decided to make this a procedure, since I needed to sync two folders so it'd be easier to call deleteSyncFiles twice, with the appropriate parameters. Note that this procedure is called as if you're backing up normally, from the source to the destination. Also note that I name the variables v_xxx (v for variable). It's a simple naming convention that I've seen a lot of people use, keeps the different variable types visually separate. Usually I see g_ for global, l_ for local, c_ for constant.

Another way to do it is to prefix the variable type, which is more important in AppleScript since variables are not typed. So s_ for string, i_ for integer, d_ for double float, etc. Finally some people combine the two, so we get gs_ for a global string varible and lf_ for a local float variable. I've found that it does keep the code clearer, so I recommend it.

More in the next entry...

Copyright (c) 2000 Kevin C. Wong
Page Created: August 17, 2004
Page Last Updated: August 17, 2004