Search for certain kind of files.

Hi everyone,

I’d like to make a script that gives me all files of a folder and its subfolders who match the specific extension. That’s quite easy but not when it comes to about searching for 100 extensions.

I could try this but with 100 extensions, it will become a mess…

do shell script "find " & quoted form of (POSIX path of (choose folder)) & " -iname *.avi -o -iname *.mov -o -iname *.mpg -o -iname *.mpeg"

Alternatively this, but it’s relatively slow…

repeat with i in ExtensionsList
	do shell script "find " & quoted form of (POSIX path of (choose folder)) & " -iname *." & i
end repeat

But I discovered this, it doesn’t search for all extensions I want, instead it gives all folders, that’s not exactly what I want but it can go with it…

do shell script "find " & quoted form of (POSIX path of (choose folder)) & " -type f"

The only problem with that is, that it gives me back system files like .DS_Store etc. I know how to filter them out of the list using AppleScript’s text item delimiters but I’d rather want to filter them with the shell script, it looks safer to me.

Well Adam or Stefan will undoubtedly come in with a mdfind version, but here’s my quick solution which utilizes a Extension List without the slowness of multiple finds… just build your messy search string on the fly!

set ExtensionsList to {"*.avi", "*.mov", "*.mpg", "*.mpeg"}
set srchStr to ""

repeat with aExt in ExtensionsList
	set srchStr to srchStr & " -iname " & aExt & " -o"
end repeat

do shell script ("find " & quoted form of (POSIX path of (choose folder)) & (text 1 thru -4 of srchStr))

Actually mdfind is a bit more nuisance in this instance because you’d have to visit examples of each extension type using mdls to discover the approprate Spotlight metadata to search for.

If you do this kind of search often you should check out Leap by Ironic Software at

Jim Neumann

Sorry if that’s considered inappropriate, Ray (or whoever is moderating this section). I do work for Ironic Software but that wasn’t meant as a sales pitch. (It’d be a pretty poor one if it was :P)


I was rather hoping for the -type f script but this turns out great too.

I tested it a bit and it seems that doing 1 search in a folder with 20 files took 0.06637 seconds. I thought it would go much slower. On the other hand <I know I haven’t asked for this, sorry :)>, since the list of extensions is already messing stuff up, doing the long shell script that’s messy too but without the extension list would be the same only this takes 0.04445 seconds. I know there is no remarkable difference to the user since the speeds are so low but I just wanted to mention it.

No BLUEFROG, I’m sorry, I don’t use those searches often… I use it to get all the contents of a folder & subfolders.

Thanks :slight_smile:

You can put the type -f parameter into my version as well if you would like.

do shell script ("find " & quoted form of (POSIX path of (choose folder)) & " -type f" & (text 1 thru -4 of srchStr))

Yes, that’s true but I actually meant

do shell script "find " & quoted form of (POSIX path of (choose folder)) & " -type f"

and then filtering the system files like .DS_Store

But it doesn’t matter anyway…

Thanks :slight_smile:

you can filter file names starting with a dot with

! -name '.*'

Oh yes, I should have know that…

Thanks :slight_smile: