Script moving on before large files have completed moving....

Hello all,
Im in a bit of a quandry. I have a shutdown script that seems to be chugging along just fine, until I noticed that the desktop cleanup portion of the script failed for large files. Im on osx 10.8.

Basically the script:

  • creates a cleanup destination folder on a partition.
  • creates a list of files that should remain on the desktop
  • copies everything not on that list to the cleanup folder
  • moves the originals on the desktop to the trash
  • empties the trash
  • fires shutdown -h +1 to shut down the machine

With large files (ie 50gb+), the finder doesn’t complete the move and in the morning the files are still on the desktop and a partial copy is in the destination folder and the trash is empty. This tells me the large item was still copying, other small items were moved and the script moved on before the large item finished.
This is better the erasing someones large files completely but not ideal.

Anone else deal with this kind of issue – what should I try to do to prevent it?

try
	if (sourceSize is not equal to desktoptSize) or (desktopList is not equal to sourceList) then -- compare lists and file size 
		-- move everying
		tell application "Finder"
			move (get items of desktop whose class is not disk and name is not in sourceList) to folder folderpath -- move everything to cleanup folder
			-- the problem seems to be with the above move comand
			move (get items of desktop whose class is not disk and name is not in sourceList) to trash -- if it doesn't move it, but copies it, send it to the trash
			
			tell application "Finder" -- eject everything now in case an open dmg boggs down emptying the trash 
				eject (every disk)
				close (every window)
			end tell
			empty trash
		end tell
	end if	
	completeShutDwn(comDesErrors)
end try

on completeShutDwn(comDesErrors)
	try
		do shell script "sudo shutdown -h +1 &> /dev/null &" -- release the atomics (in one minute)! 
		-- ( permissions for shutdown should be granted to user/group 'student' via visudo )
		-- the '&> /dev/null &' should mean that the app can move on after issueing the shutdown command (ie. quit, and update log)
		quit -- the app!
	end try
end completeShutDwn

(1) I’m not sure that the Finder is the most efficient tool to move files.

(2) You may check that the copy processes are complete before issuing the shutdown call.

To do that, compare the size of the source file to the size of the moved one.
If you use System Events or a shell script to delete the source files, there will be no need to empty the trash because these tools delete the file descriptors, they don’t move files to the trash.

Yvan KOENIG (VALLAURIS, France) jeudi 13 décembre 2012 11:55:39

Hello.

Your script sounds like a very good idea for someone I know… :wink:

One way to prevent it would be to check if the modfication date of your cleanupfolder stops changing, within 2 sec intervals, when it don’t, then the moving of files are done, and you can commence with the shut down process.

Another way is to look at the busy status of the folder.

Naturally, before starting those tests, you should delay 2 seconds, so you are sure that the requests have reached finder before you proceed.

Just wanted to thank you both for the feedback - holls took me by storm (literally) so sorry for the huge delay in responding.

I cobbled together this script to check the size of a folder and see if it is no longer changing before moving on.
I’ve been testing it by copying in a 4gb movie file and on one occasion out of 12 or so tests its exited before the file was done, which perhaps is a fluke. I realise this thread may have gone cold, and will repost it as a new question if I dont hear back, but if anyone can see a way to fail safe this Im all ears.

many thanks

set MyItem to POSIX file "/Users/anachronistic/Desktop/2test"
to checkSize(this)
return size of (info for this)
end checkSize

set oldSize to 0
set newSize to -1
-- When newSize equals oldSize, it means the copy is complete because the size hasn't changed.

repeat while newSize is not equal to oldSize
-- Get the file size.
set oldSize to checkSize(MyItem)
log "oldSize:  " & checkSize(MyItem)
delay 5

-- Sample the size again after delay for comparison.
set newSize to checkSize(MyItem)
log "newSize:  " & checkSize(MyItem)
end repeat

log "exit"
beep

adapted from http://dougscripts.com/itunes/itinfo/folderaction01.php

Here’s a good file size checker that I use. It also checks for files that go missing (ie moved by another process) because I use it for a watched folder where multiple computers pull files.


on delayUntilFileSizeStopsChanging(theFile)
		logStatement_("checking size fn")
		logStatement_("theFile= " & myWatchedFolder & myIDFileName)
		
		try
			--get initial size	
			set sizeThen to (do shell script "stat -f %z " & quoted form of (POSIX path of theFile))
			--stat gets statistics on a file, returns multiple objects separated by tabs or spaces
			--use -f for formatting and %z is the file size datum
			logStatement_("sizeThen= " & sizeThen)
			
			repeat
				do shell script "sleep 10" --wait between runs
				
				--get new size
				set sizeNow to (do shell script "stat -f %z " & quoted form of (POSIX path of theFile))
				logStatement_("sizeNow=  " & sizeNow)
				
				if sizeNow = "" then error --file is not there any more
				logStatement_("subtract= " & (sizeNow - sizeThen))
				
				if sizeNow - sizeThen is less than or equal to 0 then exit repeat
				--set old size to compare on another loop through
				set sizeThen to sizeNow
			end repeat
		on error
			error "file size check failed" number 4004
		end try
		
		return 0
	end delayUntilFileSizeStopsChanging