On Oct 14, 2008, at 12:13 PM, Joe wrote: I have a problem though with writing the last piece to a file.
I think that before the 'read until space' you'll probably need a test for 'get eof' and then a compare for the accumulated length or use an embedded try block.
Another "sketch" is below (not stress tested, will need checking). All in all, a better approach might be by reading lines and then testing to see if the accumulated length is less than 1000.
set p2d to (path to desktop folder as text) set fpath to p2d & "aaa.txt" set read_size to 1000 set accumulated to 0
try set fd to open for access fpath set read_limit to (get eof of fd) repeat set d2 to "" set d1 to read fd for read_size set accumulated to accumulated + (length of d1) if (accumulated is less than read_limit) then set d2 to read fd until space set accumulated to accumulated + (length of d2) end if set res to d1 & d2 -- do something with -> --{res, length of res} if (accumulated is equal to read_limit) then exit repeat end if end repeat close access fd on error try close access fd end try end try
This script does read the last piece of a file (as evidenced by turning on logging) but it doesn't write the last chunk to a file as it does previous chunks. It seems the error I'm trapping gets thrown before the last write is done, but I can't find a way to fix it.
--My script tell application "Finder" set myfile to the selection as alias set flname to name of myfile end tell set filcnt to 0 set fr to open for access myfile try repeat set filcnt to filcnt + 1 set rd1 to read fr for 2000 set rd2 to read fr until space set chunx to (rd1 & rd2) as string set nfr to open for access file ((path to desktop folder as string) & filcnt & " " & flname) with write permission write chunx to nfr close access nfr end repeat on error try close access fr end try end try
Philip Aker
Democracy: Two wolves and a sheep voting on lunch.
|