Moving/Trashing files based on extension

Hi,

I need to write a script that functions in this manner:

Take the name of the first file of folder1
look for a file in folder2 with the same name (before the extension)
Move this file from folder2 to the trash
Repeat with all files

In english: I have two folders with files with the same name but different extension. I need to delete the files in folder two with a “crw” extension that also appear in folder1 (with a jpg extension).

Your help is truly appreciated

Maybe this will work (minimal testing was done). You’ll need to enter the correct paths in the first two lines.

set jpgs to "path:to:jpg folder:"
set crws to "path:to:crw folder:"

tell application "Finder"
	set trash_candidates to (files of folder crws) as alias list
	repeat with file_ in trash_candidates
		set s_name to (my no_extension(file_))
		if exists file (s_name & ".jpg") of folder jpgs then delete file_
	end repeat
end tell

on no_extension(f)
	set info_ to info for (f)
	set name_ to name of info_
	set ext to name extension of info_
	set short_name to characters 1 thru -((count ext) + 2) of name_
	return short_name as text
end no_extension

– Rob

Hi Rob, thank you for your help. The script works great by itself but does not work when made into a sub-routine (which is what I need to do). Is there any way to alter it to work? Thanks alot.
complete script:
on deleteRaw()
tell application “Finder”
set jpgs to folder “Rejects1” of clientFolder
set crws to folder “Raw” of folder “Processed(originals)” of clientFolder
set trash_candidates to (files of folder crws) as alias list
repeat with file_ in trash_candidates
set s_name to (my no_extension(file_))
if exists file (s_name & “.jpg”) of folder jpgs then move file_ to trash
end repeat
end tell

on no_extension(f)
set info_ to info for (f)
set name_ to name of info_
set ext to name extension of info_
set short_name to characters 1 thru -((count ext) + 2) of name_
return short_name as text
end no_extension
end deleteRaw

Maybe this will work.

my deleteRaw("reference:to:client folder")

on deleteRaw(clientFolder)
	tell application "Finder"
		set cf to clientFolder as Unicode text
		set jpgs to folder "Rejects1" of folder cf
		set crws to folder "Raw" of folder "Processed(originals)" of folder cf
		set trash_candidates to (files of folder crws) as alias list
		repeat with file_ in trash_candidates
			set s_name to (my no_extension(file_))
			if exists file (s_name & ".jpg") of folder jpgs then move file_ to trash
		end repeat
	end tell
end deleteRaw

on no_extension(f)
	set info_ to info for (f)
	set name_ to name of info_
	set ext to name extension of info_
	set short_name to characters 1 thru -((count ext) + 2) of name_
	return short_name as text
end no_extension

– Rob

Hi Rob, thanks for your help. Unfortunately I get the following error:
Can’t make every file of «class cfol» («class cfol» “Raw” of «class cfol» “Processed(originals)” of «class cfol» “aury300ENP-DVD” of «class cfol» “Desktop” of «class cfol» “powerbookg4pp” of «class cfol» “Users” of «class sdsk» of application “Finder”) of application “Finder” into a «class alst».

I did check that the path is ok. any clues?
Thank you

Hi Rob,
I played around a little and nawrrowed down the problem

set trash_candidates to (files of folder “Rejects1” of folder cf) as alias list
does not work (same error)
but
set trash_candidates to (files of folder cf) as alias list
does work. This is where the problem is.
Thank you

I also noticed that when I re-wrote the script a little sometimes it worked when there was more than one file in the crw folder. It did not work when there was only one file

Alrighty then, we’ll eliminate the coercion to an alias list. This should also address the problem with a single file.

my deleteRaw("reference:to:client folder")

on deleteRaw(clientFolder)
	tell application "Finder"
		set cf to clientFolder as text
		set jpgs to folder "Rejects1" of folder cf
		set crws to folder "Raw" of folder "Processed(originals)" of folder cf
		set trash_candidates to (files of folder crws) as list
		repeat with file_ in trash_candidates
			set s_name to (my no_extension(file_ as text))
			if exists file (s_name & ".jpg") of folder jpgs then move file_ to trash
		end repeat
	end tell
end deleteRaw

on no_extension(f)
	set info_ to info for alias f
	set name_ to name of info_
	set ext to name extension of info_
	set short_name to characters 1 thru -((count ext) + 2) of name_
	return short_name as text
end no_extension

– Rob

thanks Rob. Now another error occurs, but further down. It highlights the word “text” and says:

Can’t make every file of «class cfol» («class cfol» “Raw” of «class cfol» “Processed(originals)” of «class cfol» “aury300ENP-DVD” of «class cfol» “Desktop” of «class cfol» “powerbookg4pp” of «class cfol» “Users” of «class sdsk» of application “Finder”) of application “Finder” into a string.
or
Can’t make every file of folder (folder “Raw” of folder “Processed(originals)” of folder “aury300ENP-DVD” of folder “Desktop” of folder “powerbookg4pp” of folder “Users” of startup disk) into a string

This works for me in OS X 10.2.8. If it doesn’t work for you then I guess we need specific info on how you are incorporating the code into your script along with the version of Mac OS that is in use.

my deleteRaw("path:to:client folder:")

on deleteRaw(clientFolder)
	set cf to (clientFolder as text)
	set jpgs to (cf & "Rejects1:") as text
	set crws to (cf & "Processed(originals):Raw:") as text
	tell application "Finder"
		try
			set trash_candidates to files of alias crws
		on error
			set trash_candidates to (first file of alias crws) as list
		end try
		repeat with file_ in trash_candidates
			set s_name to (my no_extension(file_ as text))
			if exists file (s_name & ".jpg") of folder jpgs then move file_ to trash
		end repeat
	end tell
end deleteRaw

on no_extension(f)
	set info_ to info for alias f
	set name_ to name of info_
	set ext to name extension of info_
	set short_name to characters 1 thru -((count ext) + 2) of name_
	return short_name as text
end no_extension

– Rob