Re: Monitor Folder For Changes
Re: Monitor Folder For Changes
- Subject: Re: Monitor Folder For Changes
- From: Mike Fischer <email@hidden>
- Date: Fri, 20 Dec 2002 11:29:49 +0100
Hi Frank,
Am Donnerstag, 18.12.02 um 15:10 Uhr schrieb "Frank W. Walker"
<email@hidden>:
I am trying to write a script which will monitor a folder and copy any
files which are newly added or updated to another location. So far, I
have a folder action script which launches when a file is added to the
folder. I check the creation date of files in the folder and copy
recently-created files to another folder.
There are a number of problems with this approach:
- The creation date is not nessesarily a valid indicator for when a
file was added to a folder. Consider the case of an old file moved to
the folder in the Finder for example.
- Some applications take a while to write a file. In effect you could
separate the process into the phases create a new empty file, open the
file for writing, write data to the file and finally close the file. In
addition the file attributes might be changed at any time during or
after this process. On Mac OS X and in some cases on Classic Mac OS
your app might happen to "see" the new file while it is still in one of
the phases of being constructed. (Well behaved applications create
files in the temporary items folder of the volume - if supported by the
volume - and then move them to the destination folder in a single
atomic step. But you can't rely on this.)
- Depending on your needs you might also want to detect modified files.
There are several kinds of modification: modified content (with or
without changed length of the file), modified attributes or even a
modified file name. A clear definition is needed of what constitutes a
modified file. For example: does changing the label in the classic
Finder constitute a modification?
- Also depending on your needs you might want to consider subfolders
and files moved inside the hierarchy of your folder.
- Deleted files might need to be detected and considered.
- Some people might think that comparing the item count in the folder
might be a good idea to trigger an action. While this will often work
it misses cases where an equal number of files where added as where
removed from the folder inside the time you last checked.
- Also the modified date of the folder does not always reflect changes
to the content correctly. It does seem to work if the modification
consists only of actions done by the Finder, like move, copy, and
delete (actually in the Finder a delete is a move to the trash folder).
- Remote volumes pose additional problems because there are no
universal locking mechanisms to guarantee atomic operations and there
are file systems that don't support important attributes needed to
correctly detect certain changes.
The best solution to all of these problems would be some sort of
message from the file system that you can register for. Unfortunatly
this feature doesn't exist presently. Also it would be difficult to
implement for remote volumes as it would require support in the file
transfer/sharing protocol as well as the file server software.
The second best approach is keeping a cache of the results of the
previous scan of the folder and comparing this to the current state.
Depending on your needs this can be a rather complex solution though
and probably extremly difficult to implement using only plain vanilla
AppleScript.
Things get even more complicated if you want to take subfolders and
moved files into consideration.
If you can define a limited set of features you want to support and
live with the constraints then a solution using AppleScript might be
possible. For example:
- Only the files on the first level of the folder are considered;
- It is assumed that files are placed into the folder atomically;
- Only new files are considered (not modified ones);
- The maximum number of files in the folder is limited to some
reasonable value;
- Deleted or renamed files are not detected;
The problem is this:
the files are very large so they take a minute or two to copy. During
this time, another file or files might be added to the folder, in
which case they are ignored by my script once it is done copying. Is
there some way I can avoid missing any changes to the folder as far as
new/updated files?
This looks like a basic logic error in your script. If you note the
time your script started each scan and use that time as a reference to
compare against during the scan (instead of the current time) then this
problem should go away.
I'm hoping someone has a ready-made script which does just this, since
it seems like a generally useful tool.
Some years ago I wrote an application in C++ which attempted to watch
folders in a general way and report changes by triggering appropriate
handlers of an attached AppleScript. The last relased version basically
worked but there where some bugs and performance problems due to the
fact that during development the specs had changed and the underlying
database was inefficient for the new specs.
If you are interested I can give you an URL where you can download a
timelimited demo version of Lavielle. Corona. Be warned though that it
is a 68K application, so it will only run under Classic Mac OS. Also be
warned that the app is in need of a rewrite to fix the bugs and
performance problems it currently has. I'd gladly do the rewrite but it
has to be economically worthwhile for me to do so. So far that didn't
seem to be the case.
HTH
Mike
--
Mike Fischer Softwareentwicklung, EDV-Beratung
Schulung, Vertrieb
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.