How do you delete files in a folder based on their file name?

I’ve got a video converting script in the works and it’s going to be making a lot of large files and I need to be able to clean up after it as it goes so my hard drive doesn’t fill up.

It’s set up so it demuxes .ogm files into a folder called “OGM Parts” on my desktop and then the next application converts all of the .ogg audio files that came out of the demuxing process into .wav so I would like to know how I can tell it to immediately delete the .ogg file if a file of the same name with the extension .wav exists. I’ll be using this several times for removing different types of files so figuring out this step now would be a big help. Thanks

Try something like this:

choose folder with prompt "Delete intermediate files in this folder:"
set targetFolder to result

tell application "System Events"
	files of targetFolder whose name extension is "ogg"
	
	repeat with thisItem in result
		nameWithoutExtension of me for thisItem
		if exists (file (result & ".wav") of targetFolder) then delete thisItem
	end repeat
end tell

on nameWithoutExtension for someAlias
	-- `someAlias` should be an alias or file specification
	tell (info for someAlias) to return text 1 thru ((my (offset of ("." & name extension & ":") in (name & ":"))) - 1) of name
end nameWithoutExtension

Hi,

the easiest way is to integrate the delete part into the repeat loop.

This is not real code :wink:

repeat with oneFile in theFiles open oneFile set newFile to convert oneFile repeat until conversionHasFinished delay 1 end repeat do shell script "rm " & quoted form of POSIX path of oneFile end repeat