Finder: Find files with names containing a specific word

I’m wanting to find files in a specific folder with a specific word in their name.

So for example find all the files containing ‘zebra’ like the names here:

Test File Zebra 01.zip
zebra.test.document.txt
audio_zebra_sounds.mp3

Once the files have been found I’d like to set the results as variables so that I could move them, copy them somewhere else, delete them etc.

I’ve created quite a few scripts in the past processing files I’ve selected in Finder, but I’m not sure where to start with finding files that contain a certain word.

Any help would be greatly appreciated :slight_smile:

Hi. The simplest Finder method would be to use a whose clause and “contains.”

tell application "Finder" to move my ((choose folder)'s files whose name contains "Zebra") to desktop

The following approach is the same as Marc Anthony’s, except that the files are first placed in an alias list. There’s no need for this with move or a similar command, but an alias list can be useful in other circumstances.

set theFolder to choose folder

set theString to "zebra"

tell application "Finder"
	set matchingFiles to ((every file in theFolder) whose name contains theString) as alias list
	move matchingFiles to the desktop
end tell

BTW, the OP states that he wants to “set the results as variables.” Lists are normally used for this purpose, as demonstrated above.

Thanks a lot! This works how I want:

tell application "Finder"
	set desktopFolder to (path to desktop folder as alias)
	
	set theString to "zebra"
	
	set matchingFiles to ((every file in desktopFolder) whose name contains theString) as alias list
	
	move the matchingFiles to the trash
end tell

How would I do it if I wanted to run the script through multiple specified folders? Something like the following (but this doesn’t work).

tell application "Finder"
	set desktopFolder to (path to desktop folder as alias)
	set documentsFolder to (path to documents folder as alias)
	set picturesFolder to (path to pictures folder as alias)
	
	set theFolders to {desktopFolder, documentsFolder, picturesFolder}
	
	set theString to "zebra"
	
	set matchingFiles to ((every file in theFolders) whose name contains theString) as alias list
	
	move the matchingFiles to the trash
end tell

I also tried a version using ‘repeat’ but my knowledge of AppleScript isn’t good enough to make it actually work :stuck_out_tongue:

Your first script can be reduced considerably as the desktop folder is the root folder of the Finder


tell application "Finder"
	set theString to "zebra"
	move (every file whose name contains theString) to the trash
end tell

In the second script you need a loop to process each folder separately. By the way all path to … folder expressions return an alias so the coercion as alias is redundant.

And it’s recommended to move all code which is not related to a specific application out of the application tall block.


set desktopFolder to path to desktop folder
set documentsFolder to path to documents folder
set picturesFolder to path to pictures folder

set theFolders to {desktopFolder, documentsFolder, picturesFolder}

set theString to "zebra"
repeat with aFolder in theFolders
	tell application "Finder"
		move (every file of aFolder whose name contains theString) to the trash
	end tell
end repeat

Thanks a lot Stefan, that’s just what I wanted!

The first example was more about using ‘a specified folder’ rather than actually using ‘the Desktop folder’ (so I can use different folders in different scripts), but the information you’ve added is helping me learn more and more :slight_smile: