Applescript for delete all files in a folder with a prefix..


I was wondering what an applescript equivalent to the dos command for *.

I have an applescript file which renames a bunch of “keeper” jpeg files, and it would be great to add a line at the bottom which would delete all the other jpegs.

All the files I want to delete have the prefex “DSM_SP_G_” I used to do this with a batch file on a windows machine, so all I had to do was type del DSM_SP_G_*

I was able to delete specific files using the delete command, and I could just search the folder for DSM_SP_G_ but i’d love to have it happen automatically.

Any ideas?


You need a shell script embedded in your AppleScript to do that: list the chosen folder, single out the files, delete them. For an expert shell scripted that’s a one liner.


do shell script "cd '/Users/soldtoscience/path/to/folder/' ; rm DSM_SP_G_*"

Works great thanks!!

One last question, is there a way to just point to desktop/folder so this can work on any machine? Getting rid of the users/soldtoscience and maybe putting ///desktop or something?

set currentDesktop to POSIX path of (path to desktop) --> /Users/<user>/Desktop/
set folderToClean to currentDesktop & "partial/path/to/folder/" --> /Users/<user>/Desktop/partial/path/to/folder/
set quotedPathTofolder to quoted form of folderToClean -- to avoid problem with spaces

do shell script "cd " & quotedPathTofolder & " ; rm DSM_SP_G_*"

Pay attention to the spaces in that shell command!

Edit: corrected path goof.