end of file error when reading files

I’m trying to create a script which deletes empty text files in a folder.
The code stops on

if (read file notationalpath) is “” then

with the error message Applescript Error. End of file error.
Do I need to open access for each file?
I would use the ‘cat’ command in shell to read the files, but filenames with the single quote character ’ in them create an error.

set theFolderList to list folder “Macintosh HD:Users:DJ:Library:Application Support:Notational Data DJ” without invisibles
repeat with x from 1 to count of theFolderList
set theFile to item x of theFolderList
set notationalpath to “Macintosh HD:Users:DJ:Library:Application Support:Notational Data DJ:” & theFile
if (read file notationalpath) is “” then
tell application “Finder”
delete file notationalpath
end tell
end if
end repeat

I think find and xargs works perfectly here:

set theFolder to POSIX path of (choose folder)

# find is strict to file paths so we need to remove trailing slash if any
if theFolder ends with "/" and length of theFolder > 1 then
	set theFolder to text 1 thru -2 of theFolder
end if

do shell script "find " & quoted form of theFolder & " -empty -type f -print0 | xargs -0 rm"

No, that’s only needed for writing.
One other behaviour which used to be relevant: an “opened” file is marked ‘in use’, and another app could not access it until the script issued a close command.
Don’t know if it still works that way.

Yes, an empty file does that. You could catch the error in a try/on error construct, or use the get eof command:

	if (get eof file notationalpath) is 0 then
		tell application "Finder"
			delete file notationalpath
		end tell
	end if

That’s partly true. A file can be opened multiple times by different processes in read mode, a file can only opened once in write mode. This is handled by the kernel (or an extension of it) to avoid that two processes write simultaneously to the same file. In this topic we open the file in read mode so this shouldn’t be an issue.

Thanks very much for answers… Both these scripts work. and happy christmas! :slight_smile: