Deleting file according to a PORTION of filename

Hi,

I’ve been working on scripts to handle a few functions for a local, live TV show for a ski resort. They are mostly now up and running. But I’ve moved on to some backup - “What if” scripts. And I’m stumped.

Basically the current script will build 2 QT movies from downloaded stills and save these into a specific folder naming the files “Aug30-Sat.mov” and “Aug30-Rad.mov” Then tomorrow these respective files will be named “Aug31-Sat.mov” & “Aug31-Rad.mov”…so far so good.

But here’s what I need: As a backup I want to be able to throw out one of the movies and rebuild it if one of it’s frames is bad and needs to be trashed.

So I basically need a script that will look into the specified destination folder and determine if the text “-Sat” is part of the filename of any of the files there… If so, delete the file that contains the text “-Sat” in it’s filename.

Then I’ll build another script that determines if there is a file in said folder that contains the text “-Rad” and throw that one out. Pretty easy - once I’ve built the first rendition.

Right now it goes like this:

tell application “Finder”
set FirstFile to name of first file of folder “(path to folder here)”
if fileName of FirstFile contains “Sat” then move FirstFile to trash
end tell

But it returns the message: "Finder got an error: Can’t get “(file in question)”

I’m still way off here…Anyone got any ideas on this ? Thanks in advance!!

Hi,

This script will delete every file in the chosen folder whose name contains -Sat

set myFolder to (choose folder)
tell application "Finder"
	delete (files of myFolder whose name contains "-Sat")
end tell

You can change myFolder with folder “(your folder path)”.

Best wishes

John M

Can you add an or so it does both like this?:

set myFolder to (choose folder)
tell application "Finder" to delete (files of myFolder whose name contains "-Sat" or name contains "-Rad")

I tried the suggested code and end up with the error message: “…Can’t get every file of “Macintosh HD:Users:tinpanalley:Data:Another Heavenly Morning:Animated Loops-Todays”…”

I don’t know what’s going wrong here. The file in question: “Aug30-Sat.mov” lives inside the folder “Animated Loops-Today” Here’s the exact code:


set myFolder to ("Macintosh HD:Users:tinpanalley:Data:Another Heavenly Morning:Animated Loops-Todays")

tell application "Finder"
	delete (files of myFolder whose name contains "-Sat")
end tell

Try ‘delete (files of folder myFolder whose name contains “-Sat”)’, Kevin.

(You might also want to check whether the folder named “Animated Loops-Today” ends with an s.) :slight_smile:

kai,

the folder name did in fact end with an ‘s’.

however, adding the word “folder” to the delete command line did the trick.

thanks for your input!

Just to clarify a little, since this sort of confusion seems to be fairly common…

In John M’s script, the class of the variable ‘myFolder’ was an alias (the result of the ‘choose file’ command). In your version, it was a string - and Finder doesn’t recognise that a mere string is meant to be a path. What’s really needed is something Finder can readily recognise and locate, like an alias or a Finder object - such as a file or a folder. :slight_smile:

I’m glad I found this thread! I’m trying to use the code provided to basically do the same thing as Kevin. One difference is that I’m going to use this script on many machines, each with different user home directory names. I can’t seem to figure out how to get around hard coding the path including the user home dir.


set myFolder to (":Users:pilot:Library:Preferences:Adobe InDesign:Version 2.0")
tell application "Finder"
	delete (files of folder myFolder whose name contains "Defaults")
end tell

I don’t necessarily care if it tries to sweep all of the user home dirs on the machine :slight_smile:

Any input would be great. Thanks…

feanor, try this:

tell application "System Events"
	repeat with thisUser in (every folder of (path to users folder) whose name is not "Shared")
		delete folder ((path of thisUser as string) & "Library:Preferences:Adobe InDesign:Version 2.0:Defaults:")
	end repeat
end tell