noobie - folders - remove unwanted files

Hi There!

Someone wrote some script for me a long time ago.
I have misplaced the contact details.

Essentially, the existing script allows you to choose a folder and remove unwanted files (files with a particular extension)
Why? I use a Canon 10D and when it creates files in RAW format, it also writes a .thm file which is unwanted.
The simple script allows you to select a folder on a prompt and remove files from that folder.
It works great!

What I want to do now is apply this script to a number of folders - up to 60 folders instead of running the one script 60 times!

Folders are always named sequentially and in the same way:
untitled folder, untitled folder 2, untitled folder 3, …, untitled folder 60
and are placed on the desktop or in a specifically named folder on the desktop.

Here is the script I have:

set myFolder to choose folder with prompt "Select the folder containing the files you want to remove."
tell application "Finder"
	set myFiles to files of myFolder
	set i to 1
	repeat while i < (count of myFiles) + 1
		if text ((offset of "." in (item i of myFiles as string)) + 1) through ((offset of "." in (item i of myFiles as string)) + 3) of (item i of myFiles as string) = "thm" then delete (item i of myFiles as string)
		set i to i + 1
	end repeat
end tell

I hope this makes sense and I’m sure it is easy to do, however, I am stupid!


That script is somewhat verbose for what it does. Not least of which is the way it determines the ‘.thm’ files (searching for ‘offset of’ is inefficient and actually prone to errors (what would happen if the file was ‘some.file.thm’?)).

The only question is how you want to identify the folders in question - do you want to select them all and drop them on the script? or are they all in one folder and you want to nominate the top folder and have it search through the subfolders?

I’ll assume for now the latter:

tell application "Finder"
set topFolder to choose folder with prompt "Select the folder containing the files you want to remove."
repeat with eachFolder in (folders of topFolder)
delete every file of eachFolder whose name extension is "thm"
end repeat
end tell

Thanks muchly for that!

The script looks good, however, when I run that script it returns:

Finder got an error: Unknown object type.


May have been premature then …

When I run the script and select the top folder, nothing much seems to happen at all.



I recommend to use the shell for this task. It’s much faster than the Finder

set theFolder to POSIX path of (choose folder with prompt "Select the folder containing the files you want to remove.")
do shell script " /usr/bin/find " & quoted form of theFolder & "  -name '*.thm' -exec rm {} \\;"


That works for the first or top folder, however, it does not apply to the other folders.


Sorry, my mistake - It works fine!

Thanks again.


Just choose the parent folder