Test if a file has finished copying or writing to a folder

Hi - I cant belive such a simple task has caused me so much trouble. I want to assign a folder action to ftp a list of files once they have finished writing to a folder. I have tried to use the FolderAction_isDone script that is listed in the code exchange, but that doesnt do anything when I attach it as a folder action. Does anyone else have any ideas about testing files to see whether they have finished writing. I have seen comments about trying to open the file for write access and testing for a true result. I just dont know how to get this into a script.

Thanks in advance.


on adding folder items to this_folder after receiving added_items
 --Script
end adding folder items to

http://bbs.applescript.net/viewtopic.php?id=12878
Post on a folder action that checks filenames against destination folder before moving.
SC

What I was getting at was, I was not sure how to avoid starting to ftp a file that had not yet completed writing to the folder with the folder action.

Since Apple’s “after receiving” phrase really means “after starting to receive”, it’s a major pain.

The solutions posted in the past have centered on checking the size of the file and declaring it “received” when the size does not change over a couple of seconds.

Now obviously the Finder has a way of telling when the file is finished being copied into a folder, but it does not respect that in the “after receiving” phrase - the “on adding … after receiving” handler is triggered without confirming that the file is in fact received.

It basically makes any Folder Action that wants to process files dropped into a folder or sent to a folder by another app or script, useless.

Does anybody else have any ideas ??? Surely this is not the first time this has been attempted ???

I have seen comments around about making an attempt to open the file for exclusive write access, then, if it is successfull you know it has finished copying. I am just bewildered that this is such an issue, and not only on mac, we have done some developing in c# .net, and the same thing, you have the option to poll at regular intervals, or attempt to open the file exclusively. Seems plain stupid, I mean in 2005 we dont know how to work out when a file has finished copying !!! Its almost funny !

I’m not sure if the “FolderAction_isDone” code works as a folder action, but I’ve been using a tweaked version of the _isDone handler for a while and it’s worked flawlessly.

Are you sure you are setting it up correctly?

-N

If you know how long on average it takes for the file to write, maybe you could set a delay before FTP.


on adding folder items to this_folder after receiving added_items

--folder action

delay 15--waits 15 seconds before next lines
 
--FTP the file
end adding folder items to

SC

I thought the name of the file sort of gave it away as a folder action ??? Can you please let me know how you are making use of the isDone() script ?? Thanks heaps.

What I meant by “I’m not sure if the “FolderAction_isDone” code works as a folder action” is that I’ve never used that code as posted, so I can’t say if it works.

I use the isDone handler to do just what it’s meant to do…verify that a file has been fully written to before subsequent processing begins. If I remember correctly, I had to do mod to the file size call because I was getting a some kind of a stall at around 750mb. (can’t quite recall off of the top of my head).

-N

Cool - are you able to post the script you have that calls isDone() ?

Are you using it as a folder action ?

You could put all the FTP related code inside the [Tell app “Finder” to copy…] block. That way until Finder is not done, it will not allow the script to carry on… Something like:


set TheFile to choose file without invisibles -- choose a larger file so that you notice what I mean (so that it takes some time)
set TheFolder to path to desktop from user domain

tell application "Finder"
	copy file TheFile to folder TheFolder
	tell application "iTunes" to play
end tell

That depends of course how it is you’re copying the files… If someone else is dropping them in one of your folders or whatever the use of networks someone can give nowadays… :wink:
But I think the Finder would have something to do anyway… Of course, your script will be stuck until Finder finishes the copy… not so good…

Hey,

I just tried out the “FolderAction_isDone” folder action. It’s works fine as is on my machine. I didn’t have folder actions activated before, and it did seem to take a couple of tries of “on/off” toggling to activate the folder actions.

Are you sure you’re “active”?

-N

I wrote a stay open script application that watches a folder in a similar way to folder actions except that it attempts to determine when the file copying is complete.

http://scriptbuilders.net/files/folderactionreplacement1.0.0.html

Determining when copying a file is complete is non-trivial and that explains why in 2005 we still have issues with it. Copying a large file or copying a smaller file over the network will result in lots of file open and closes. Relying on a test to see if you can write to a file is unreliable.

I wrote the above script to try and deal with this. To use the script your will need to fill it in with your own code in the ProcessThisFile handler. The script will ignore any file with the same name as one it has already processed and the script relies on the file size remaining constant for a certain amount of time before deciding to process this file.

Kevin

I’ve been using this. Works for me, unless a very large file comes in through a slow connection.
This folder action script gets the size of the file when it first appears then again a few seconds later.
If the first file size doesn’t match the second, then it loops. When the sizes do match, then it duplicates
the file to another directory. Maybe it can be adjusted a little to work for you.

property destinationFolder : “hardrive:photoftp:”

on adding folder items to this_folder after receiving these_files

repeat with ThisFile in these_files
	
	repeat
		tell application "Finder" to set theString to name of ThisFile as string
                 ---display dialog theString giving up after 1
		
		get info for ThisFile
		set sizethen to (size of the result) as number
		if sizethen is 0 then
			---display dialog "File size not started to increase" giving up after 2
		end if
		
		get info for ThisFile
		---display dialog sizethen giving up after 1
		delay 3
		get info for ThisFile
		set sizenow to (size of the result) as number
		---display dialog sizenow giving up after 1
		if sizenow is greater than sizethen then
			---display dialog "file is growing" giving up after 2
		end if
		if sizenow is equal to sizethen then
			---display dialog "GOT A SIZE MATCH" giving up after 1
			
			tell application "Finder"
				duplicate ThisFile to folder destinationFolder with replacing
			end tell
                       exit repeat
		end if
		
	end repeat
end repeat

end adding folder items to

on adding folder items to theFolder after receiving addedItems
stable(addedItems)

-- The rest of your wonderful, wonderful script...

end adding folder items to

on stable(addedItems)
   tell application "Finder"
       repeat with theItem in addedItems
           -- Mr Comment sez : "Aare u Copieink teh Fileh?"
           repeat
               try
                   get info for theItem
                   set sizethen to (size of the result)
               on error theError -- Stuff went wrong, master
                   display dialog theError
                   error number -128 -- break it off if stuff went wrong!
               end try
               delay 5
               get info for theItem
               set sizenow to (size of the result)
               if sizethen = sizenow then
                   if sizenow = 0.0 then
                       error number -128 -- break it off if stuff went wrong!
                   end if
                   exit repeat
               end if
           end repeat
       end repeat --Waiting for all files to become stable
       
   end tell
   
end stable

The same principle as Jimmyjay’s, but in a handy subroutine form. You can include it into any folder action, and it quits if stuff goes wrong. I can’t write a Folder Action without it :slight_smile:

Thanks Tieke. Nice touch up.
I’m pretty new at this game. Great example of how I could have used subroutines.
Lesson learned and appreciated. :smiley: