I need script to do selective deletes

I would like a script that I can attach to a button to do the following.

I have several file sets. All have the same file name but a different extension. I would like to select a bunch of files with various extensions, then click a button to activate a script to delete each of the selected files IF AND ONLY IF there is not a file with the same name (in the same folder) with the extension I can hard code into the script.

Example. I select 20 files, some with the extension XXX others with the extension YYY. If a file exists in the same folder and with the same name but with the extension (let's say AAA) that I have hard coded into the script, then that file will remain. However if there is no file with the same name as the selected file (with the extension AAA), then that selected file will be deleted.

Can you help me with this? In case you are wondering, I have a bunch of photos in a raw format with some sidecar files. If I delete some photo(s) I want to be able to delete all the sidecar files associated with the deleted photos.

If I've interpreted your requirements correctly I think this script will do what you want. Let me know if I've misunderstood something.

1000.xxx and 1003.yyy will be deleted. 1001.yyy and 1002.xxx will remain because they have matching .aaa files.

MagicExt = ".aaa"

Function OnClick(ByRef clickData)
	Set cmd = clickData.func.command
	cmd.deselect = False

	Set setMagic = DOpus.Create.StringSetI
	
	For Each file In cmd.files
		If file.ext = MagicExt Then
			setMagic.insert(file.name_stem)
			cmd.RemoveFile(file)
		End If
	Next

	For Each file In cmd.files
		If setMagic.exists(file.name_stem) Then
			cmd.RemoveFile(file)
		End If
	Next

	' remove this loop once you're satisfied the script works correctly
	For Each file In cmd.files
		DOpus.Output "Will delete: " & file
	Next

	' uncomment this line to delete for real
	'cmd.RunCommand("delete")
	
End Function

Jon,

If I add code to a button and run as "standard function, Opus or external" I get the error: "Windows cannot find 'MagicExt'. Make sure you typed the name correctly and then try again". If I run it as Script Function with function type VBSCRIPT. nothing happens.

Script Function, type VBScript, is correct. Are you selecting files before clicking the button?

Also as it is that script will only print out the names of files to be deleted (as shown in my screenshot), you need to uncomment the delete command once you've verified it's doing what you want.

Jon,

Yes, I select some files with different extensions. When I click the button the selection (highlight) disappears but there is no prompting whether to delete the file and all the files are still there. Is there any other parameter for the button that needs to be set? I don't understand the Opus specific functions but it appears that you are building a table of filenames (without extension) then going through that table to see if there is a file in the folder that matches the filename and has the extension I entered for MagicExt. I edited the script (in the button) to try to play around with it. When I did that it got some results - but ONLY using F5 while in the button edit! Maybe there was some garbage from the copy/paste I did to put your code into the button. The files in the list WERE correct! After I did the edit, the selected files were no longer being unselected. So maybe the code was not being executed at all before that. However, when using the button in the normal environment (not in customize) nothing happens - no list is displayed like is shown in the customize environment.

Turn on the script log from Help / Logs / Script Log and see if anything is printed there when you click the button.

Jon,

I figured out that some window must be missing when I am running in the real environment. I used the help file to look up the command you used to print the file names and saw that it is was writing to the script log. I found the menu to enable that, and was able to see the same output that I saw in the customize environment. When I got back here to tell you that, I saw your reply to use the script window. So I think we are good. I just have to remove the test loop and the comment on the delete and test again. Thank you.