Choose all files based on name?

I’m trying to create a script that will select all files in a folder that have a specific text in the name (example: I have audio file imports that will have a 0001_12.wav and 0001_34.wav version. I want the script to select all the files containing the “_34” so that I can delete them all quickly and easily).

First I tried:

tell application "Finder"
	set matchlist to every file of targetfolder whose name contains "querystring"
end tell

but I get the error: error “The variable targetfolder is not defined.” number -2753 from “targetfolder”

I changed it to:

tell application "Finder"
	set sourceFolder to (choose folder with prompt "Choose the source folder:")
	set matchlist to every file of sourceFolder whose name contains "querystring"
end tell

but now nothing happens after the popup that lets me choose the folder.

I would prefer the script to run automatically based on the current folder I have selected (and not force me to choose the folder each time), but anything working would be better than what I have now.

Thanks for any tips!


try this it specifies the folder depending on the selection

tell application "Finder"
	set selectedItems to selection
	if selectedItems ≠ {} and class of item 1 of selectedItems is folder then
		set matchlist to every file of item 1 of selectedItems whose name contains "querystring"
		display dialog ((count matchlist) as text) & " files found" buttons {"Cancel", "OK"} default button "OK"
	end if
end tell

Thank you for the reply!

After running this script it produces a popup that says “0 files found.” The result of the script says “{button returned:“OK”}” but the querystring command did not let me type what the search should be for

edit: and just to clarify I don’t need the script to count or tell me how many files match the search, I would just like them all selected so after running the script I can easily delete the ones I don’t need.

Thanks again

the dialog window was only to indicate that something happens at all
Try this, you will be prompted to enter the query string and to choose the folder and then all matching files will be selected

set queryString to text returned of (display dialog "Enter query string" default answer "")
set theFolder to choose folder with prompt "Choose folder"
tell application "Finder"
	open theFolder
	select (every file of theFolder whose name contains queryString)
end tell

Hi. You’ve assumed that “querystring” is a command or a declared variable, but you should understand that it’s just placeholder text.

This worked perfectly, thank you!

That was definitely my problem, I was looking through a similar script and it had that line so I assumed querystring would actually bring up a text promt