property destFolder : "Macintosh HD:Dest Files"
set sourceFolder to "Macintosh HD:Source Files"
moveFilesFrom(sourceFolder)
on moveFilesFrom(thisFolder)
tell application "Finder"
-- use a 'try' statement since the get files line will throw an error if no files found
try
set theFileList to every file of folder thisFolder whose name contains ".exs"
repeat with aFile in theFileList
move file eachFile to folder destFolder
end repeat
end try
-- now check for sub-folders:
-- again, use 'try' since the get will fail if there are no sub folders
try
set theFolderList to every folder of folder thisFolder
repeat with subFolder in theFolderList
-- if we have a subfolder, recursively call ourselves
my moveFilesFrom(subFolder as string)
--now check if the folder is empty
if number of files in folder (subFolder as string) = 0 then
delete folder subFolder
end if
end repeat
on error theErr
display dialog theErr & " occured when processing " & name of subFolder
end try
end tell
end moveFilesFrom
Note the code isn’t heavily tested (since I don’t have a folder full of .exs files), but it should be pretty easy to work out any parts that don’t work.