Search a folder for a file that contains a string and delete it

I want to search a particular folder for a file that contains a certain string within its name. And then delete that file. For example. Search a folder on my icloud drive and if any filename contains “_Data”, then delete that file.

Ex Folder: “/Users/Joe/Library/Mobile Documents/com~apple~CloudDocs/Stuff/”

Ex Files:
Test.jpeg
build12.xlxs
G33762.doc
homed.text
56_Data.numbers

So I want to delete the file “56_Data.numbers” because it contains “_Data”

This is what I have. All my efforts to insert code to make it work have failed.

Any suggestions? Thanks

property hFolder : “/Users/Joe/Library/Mobile Documents/com~apple~CloudDocs/Stuff/”

set newFileList to list folder hFolder without invisibles
repeat with m from 1 to number of items in newFileList

if (m as text) & "_Data.numbers" is in newFileList then
		
else

end if

end repeat

Are you tight to AppleScript?
If not, such operation can be done using shell script:

In simple form:

NAMEPART="_Data.numbers"
rm *${NAMEPART}*

Yes, I’m trying to incorporate this into an existing applescript.

why not prepare command string and execute using

set theCmd to  "rm *" & thePartName & "*"
do shell script theCmd

Nutilius
Thanks for the tips.

I found the solution I was looking for in an older thread from Mar 25. See Below

AppleScript - Deleting selected files and folders whose filename contain a certain string