Problems in deleting files

I have to run this script daily on 500 small text files out of which 400 get deleted.
When I use this script, all the files are properly deleted but the Activity monitor shows my Finder’s CPU usage at maximum (100% of CPU is used by Finder) and even after the script finishes executing, the usage remains at maximum.

set thedellist to {"0", "595", "606", "14", "592", "10", "18", "22"}
set ifolder to "<some folder>" as alias
tell application "Finder"
	set allitems to every item of ifolder
	repeat with aitem in allitems
		set x to size of aitem
		set x to x as text
		if x is in thedellist then
			move aitem to trash
		end if
	end repeat
end tell

So I replaced the above script with this one:

set thefiledellist to {}
set thedellist to {"0", "595", "606", "14", "592", "10", "18", "22"}
set ifolder to "<some folder>" as alias
tell application "Finder"
	set allitems to every item of ifolder
	repeat with aitem in allitems
		set x to size of aitem
	set x to x as text
		if x is in thedellist then
					set aitem to aitem as text
			set end of thefiledellist to aitem
			end if
	end repeat
end tell
repeat with oneitem in thefiledellist
	set qaitem to quoted form of (POSIX path of oneitem)
	do shell script "rm " & qaitem
end repeat

This script does not delete the files properly. It deletes some 10-15 files. I run it again. It again deletes some 10-15 files. I also added a delay after “set x to size of aitem” but still it only deletes 10-15 files. My CPU usage remains low when I use this script.

Help please!

The Finder is rather slow and if your list is long, the script may simply be timing out. In your place I’d get the list of files as an alias list, convert those to posix paths and use “rm” to dump them. See man rm.

Thanks for the reply. I think, I am doing what you said in my second script in the post above. Did you read it?

Is your criteria really size based? Are they specific values as you’ve shown here or in a range (less than 8K, for example)? If it is then walking the folder is not a very efficient way to build your deletion list. You could try something like this:

tell application "Finder" to (every file of (choose folder) whose size is less than 8000) -- I left off the delete after 'to' so you wouldn't accidentally do something bad - lol
-- You could actually add the delete in there and have a one-liner that would delete every file in the chosen folder that's less than 8K.

Be cautious when using item as this will pick files and folders. I suggest being more specific unless you want to work with either.

On another note: It sounds like you may also have a workflow problem. Are these 500 files generated throughout the day? By whom? Do you have access to purge them throughout the day or do you have to process them in a batch like this? Seeing as you’re deleting 4/5 of the files - are these files even necessary or are they just “how things have always been done”?

I design workflows (and do the scripting and support) for a living. Many times it is better to create a new workflow than to try automating an inefficient existing one. One of the quickest ways to solve a scripting problem. make a better workflow that doesn’t require cobbling together a broken script. Just some food for thought.

Cheers,
Jim Neumann
BLUEFROG

Hi,
Thanks for the reply.

Yes, those downloaded files which are actually missing links contain different texts. Some say “file not found”, " file not in archive" etc which all have different sizes which is why I have created a list of those sizes.

There are files of the newspaper that i download. Since, I cannot be certain about how many articles can be on each page, there are those extra files. Also, on certain days in a week, the number of pages change. I am not planning to write a separate script for each day.
Also, the entire download is completed in less than 3 minutes. So deleting the unwanted files is faster than bothering to test whether the file exists and then decide whether to download it or not. The best, I can do is: add code in the script which downloads the newspaper to delete the file as soon as it is downloaded if the size is in the list of sizes. But I can do that once I discover a reliable way to check the file size and then delete the unwanted files. That is why, I am running this separate script and I need help with it.

You’ve got me curious, Chris. I am unfamiliar with “downloading a newspaper”. Is there a URL I could check out or is this an internal thing in a workplace?

Jim