Apple Script Delete

Hi All,

I know you can do a delete in applescript by doing:


set tFolder to alias "Media 1TB:My Files:Junk Folder"
tell application "Finder"
   delete (files of tFolder)
   --    delete tFolder
end tell

But how do you get it to not delete a File or a Folder inside the above, delting all the others?

e.g

Folder to Delete

  • Sub1
  • Sub2
  • Sub3

I want the script to delete Sub1 and Sub3 but not Sub2…

Any help is greatly appreciated.

Hi JayBird,

Use the filter reference form:

set tFolder to alias "Media 1TB:My Files:Junk Folder"
tell application "Finder"
	delete (files of tFolder whose name is not "Sub2")
	--    delete tFolder
end tell

If Sub2 is a file, then you need to add the extension.

Edited: and btw. If you don’t want to delete several files or folders, then you can use a list. i.e.

items of tFolder whose name is not in {"Sub2", "File2.txt", "Sub5", "File5.rtf"}

gl,
kel