GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Rename and overwrite button

I use a photo program which will not overwrite existing files which means, I end up with new files with numbered suffix such as filename 1.jpg, filename 2, jpg, filename 3.jpg etc.

The simplest and quickest way "around this" has been to do a batch rename, remove the space and number at the end and then replace (delete) the original file.

Can I implement this in a button so that all I have to do, is to press once and have it do the rest without user input?

Edit: I found this, which is nice: Remove characters from selected folder and file names

One thing is lacking, which coincidentally also is th biggest reason for me to create this post: To auto-replace/delete existing files. Right now I have to do it for each file.strong text

Here's a button which should do what you want:

Test JScript.dcf (7.0 KB)

See How to use buttons and scripts from this forum for what to do with the .dfc file.


Using the button the .dcf file creates:

Select the files you want to rename (or just do a select-all), then click the button. It will:

  • Ignore folders, and files that don't end with a number (before the extension).
  • For files that do end with a number:
    • Check if something already exists with the name minus the number.
    • If a folder already exists, the file will be left as it is.
    • If a file already exists, it will be renamed to Filename OLD_VERSION.ext, or Filename OLD_VERSION (2).ext and so on if one already exists.
    • The new file will then be renamed to remove its number.
  • Finally, all files with OLD_VERSION in their names will be selected. You can then inspect the results and, if you are happy, click the Delete button. (You could make the script do this automatically, of course. I figured you might want the opportunity to see what happened before doing anything destructive.)

Note that the files are processed in the order they are shown in the lister. The last file will win, if there are several numbered versions of the same name. So make sure the lister is sorting them correctly. For example, it is optional whether Opus sorts files as this:

  • File 1
  • File 2
  • File 3
  • File 10

Or this:

  • File 1
  • File 10
  • File 2
  • File 3

If you have Opus configured to the second mode, then File 3 would win, not File 10, which would not be what you want. (If you want Opus to sort that way, the script could be re-written to cope with it, but it would make things a lot more complicated.)

Similarly, if you have the files sorted by date, then the newest or oldest file would win, depending on which way you were sorting.


Here's the script code, which is also in the .dcf above:

function OnClick(clickData)
{
	var cmd = clickData.func.command;
	cmd.ClearFiles();
	cmd.deselect = false; // Prevent automatic deselection

	var re = /^(.*) [0-9]+$/;
	var fsu = DOpus.FSUtil;

	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext())
	{
		var oldNamePath = eSel.item().RealPath;
		var nameStem = oldNamePath.stem;
		if (nameStem.match(re))
		{
			var newNameStem = nameStem.replace(re, "$1");
			var newNamePath = fsu.NewPath(oldNamePath);
			newNamePath.stem = newNameStem;

			//DOpus.Output(nameStem + " -> " + newNamePath);
			var typeExisting = fsu.GetType(newNamePath);
			if (typeExisting === "file")
			{
				cmd.RunCommand("Rename \"" + newNamePath + "\" PATTERN * TO \"* OLD_VERSION\" AUTORENAME IGNOREEXT");
				typeExisting = fsu.GetType(newNamePath);
			}

			if (typeExisting === "invalid")
			{
				cmd.RunCommand("Rename \"" + oldNamePath + "\" TO \"" + newNamePath + "\"");
			}
		}
	}

	cmd.RunCommand("Select \"* OLD_VERSION*\" DESELECTNOMATCH");
}
1 Like

This is absolutely awesome. A thousand thanks to you, sir!