How can i move a folder that contains a certain file extension

Hi everyone

I’m new to apple script and i’ve been trying to write a script that will allow me to move folders from my “downloads folder” to my “movies folder”, “music folder” etc. I’ve been trying to accomplish this by trying to move all folders from the “download folder” that contain, for instance, “avi” and “mp4” file extensions to the “movie folder”.

I’ve written this code to try and do it but its not working, i’m not sure but i think it may have something to do with the if clause

tell application “Finder”
set main_folder to (choose folder with prompt “Choose the main folder”)
set dest_folder to (choose folder with prompt “Choose destination folder”)
set sub_folders to folders of main_folder
set folderfiles to files of sub_folders
repeat with each_folder in sub_folders
if (each_folder contains folderfiles whose name extension is in {“avi”, “jpg”}) then move sub_folders to dest_folder with replacing
end repeat
end tell

If anyone has any ideas on this script or perhaps another way that i could easily do the same thing i would be very grateful

Thanks

Hi,

welcome to MacScripter.

First of all, please post your questions about the Finder in the AppleScript | OS X section of the forum.
Code Exchange is for sharing working code.

Try this


set main_folder to (choose folder with prompt "Choose the main folder")
set dest_folder to (choose folder with prompt "Choose destination folder")
tell application "Finder"
	set sub_folders to folders of main_folder
	repeat with each_folder in sub_folders
		set filteredFiles to (files of each_folder whose name extension is in {"avi", "jpg"})
		if filteredFiles is not {} then
			move each_folder to dest_folder with replacing
		end if
	end repeat
end tell

Hi Stefan

That worked great!

Thanks for the advice i’ll make sure i post in the AppleScript | OS X section of the forum in future.

Cheers

Graeme

I moved it.