Changing filename, overwrite only older

My OS 9 Classic publishing system outputs PostScript files to a MyMac : PS folder on my iMac booting into OS X, with a format that looks roughly like this:

23.45Foobar@1
23.55Foobar@2
23.58Barfoo@1
1.23Foobar@1

The first characters are a time (but not date) stamp (e.g., 11:45PM, 1:23 AM). The ‘Foobar’ or ‘Barfoo’ represents the actual source document name. The last character represents the page that was printed from the document. Note that the first and last filenames indicate that page 1 of Foobar was output twice, presumably because a problem was found and edited after the initial Postscript output. We only want to keep the latest version, whose filename in this case shows an earlier time stamp but the actual time stamp on the file itself will show that it is from a later date.

I need a script that’ll do the following, in the process of copying these files to a new MyMac : ToDistill : In folder.

MyMac : PS : 23.45Foobar@1 => delete or replace if already in MyMac : ToDistill : In, as it is superceded by the next file
MyMac : PS : 1.23Foobar@1 => MyMac : ToDistill : In : Foobar@1.ps (rename and move, replacing if necessary)
MyMac : PS : 23.55Foobar@2 => MyMac : ToDistill : In : Foobar@2.ps (rename and move, replacing if necessary)
MyMac : PS : 23.58Barfoo@1 => MyMac : ToDistill : In : Barfoo@1.ps (rename and move, replacing if necessary)

The script will have to run continually, to keep Acrobat Distiller supplied with fresh PS files to chew on. The incoming PS files are quite large, and take a while to finish saving to my MyMac : PS folder, so I have to avoid trying to rename and move them before they’re closed. I also may or may not get later (edited) versions of a given file during a given scan of the MyMac : PS folder, but have to ensure that I always use a later one if it exists.

I’m considering scanning the PS folder to make an array of filenames plus some numeric representation of their date+time (e.g., seconds since whenever) plus the proposed new name for each file. I’d then iterate through the list of proposed new names, seeing whether any of the later proposed names in the list were identical. If so, if the later one in the list had an more recent date+time stamp, I’d delete the current one and move to the next file in the list. If there were no identical names later in the list, I’d rename the file locally to the name I wanted it to have, and then move it to the MyMac : Distiller : In folder, replacing any file already there by the same name, and move to the next file in the list.

So, my main questions:

  1. Any way besides checking file size growth over time to tell if a file is closed? E.g., file type? If so, how to test?
  2. Elegant way of stripping the 4 or 5 timestamp characters off the front of each filename? Simple with regex, but . . .?
  3. Building arrays (e.g., each row has Actual name, proposed name, dateTime value) or work-alikes?
  4. Does the move + rename have to take two discrete command steps? I.e., rename the file then move it, or vice versa?
  5. Given a file by the same (re)name may exist in target directory, any advantage to the order of move+rename?

Hope someone finds this either familiar enough to be a cake walk, or interesting enough to produce some worthwhile and entertaining puzzling!

Thanks.

  • David

Model: iMac
Browser: Safari 312
Operating System: Mac OS X (10.3.9)

You don’t have to rely on the time stamp in the file name. You can script the finder to get the file’s creation date and modification date. You could attach a script to the folder so that it could automatically check for older versions each time a file was added to the folder. You wouldn’t want to mess with a file while it was being distilled, though. I would use regex to parse the file name, as you suggested. Post what you’ve got so far if you need help.

Good point about not messing with the (now outdated) file as it’s being distilled. I’d forgotten about that possibility. Guess I’ll have to check for the file being open before attempting to overwrite it.