Rename charecters and spaces with underscores

Hi
I have created the space to underscore button as explained in the forum by changing the underscore to space rename button. However is it possible to make it replace all characters including removing leading characters. eg
_test File__Four.txt
to
Test_File_Four.txt
I looked at the regex Make Safe Name expression which seems close but am not confident what to do.
Thanks

Easiest is to have a button with several rename commands. If you want to use the dialog with the preview, a rename script would get the job done.

Ultimately I was looking for a right click - Rename, but noticed the rename buttons and suspected that might be simpler. No requirement for the preview.

was looking for a right click - Rename

That's just a different place for the button: toolbar menu vs. context menu.

Ok So I edited the Make safe renamer
(.*)([^0-9a-zA-Z._]+)(.*)#
\1_\3
Basically removed "-" "," and "."
which gets me most of the way there
however how to remove a double "__" underscore and replace with a single "_"

This regex will replace multiple underscores with a single underscore:

Mode: Regular Expressions
Old Name: (.*)__(.*)#
New Name: \1_\2

(The # on the end is an Opus extension which means "repeat the regex until the string stops changing".)

This one will remove any underscores at the start or end of the name:

Mode: Regular Expressions
Old Name: ^_*(.*?)_*$
New Name: \1

If you want to combine them all together into one rename preset/button/menu/hotkey/whatever, a script is best, since then the final names get calculated directly and the files only get renamed once.

Javascript's regex are slightly different to Opus (no two regex systems are quire the same), but here's the script version that combines everything together (including your regex from the previous reply):

function OnGetNewName(getNewNameData)
{
	// Opus 12 and above handle splitting out the extension for us.
	// Need to convert Variant strings to JScript String objects so we can use String methods.
	var strExtension = String(getNewNameData.newname_ext_m);
	var strNameOnly = String(getNewNameData.newname_stem_m);

	// Apply our regular expressions to the filename.

	while(true)
	{
		varOldName = strNameOnly;
		strNameOnly = strNameOnly.replace(/([^0-9a-zA-Z._]+)/g, "_");
		strNameOnly = strNameOnly.replace(/__/g, "_");
		if (strNameOnly == varOldName)
			break;
	}
	strNameOnly = strNameOnly.replace(/^_+/, "");
	strNameOnly = strNameOnly.replace(/_+$/, "");

	// Rejoin the name and extension and return the result.
	return strNameOnly + strExtension;
}

You can save that in a Rename Preset and then refer to it in buttons/context menus via
Rename PRESET="My Preset"

It'll also automatically appear in the menu that's part of the Rename button on the default toolbars, once created.

Outstanding, many thanks.
So I am now trying to attach it to the context file menu.
Select title/tool bar - Customise - Context - Menus - Lister Content
and have added a context menu called DO_Rename.

I have then added the Jscript to the JScript screen (is this right)?
So the DO_Rename menu item appears when I right click on the lister window.
It doesn't appear when I right click file which would be my preference is this possible.
And It does not operate on the file using this method so I am going wrong somewhere.

Head over to the File Types context menu:

Thanks
however having a look there I note when I select edit for the current rename inline menu it has a different menu selection than when I select new
The edit displays a script box and is set to standard function(opus or external)

The new displays a different 4 choice menu so not sure where to go from there?

You don't need to, and shouldn't, create the script directly in the context menu. Save it as a Rename Preset instead, which you can then use from any place you need it (including in the Rename dialog itself).

  • The script should be saved as a Rename Preset, within the Rename dialog.

    See How to use Rename Presets from this forum, and the Rename Scripts (not inside a Preset) part specifically, for how to do that.

  • Once the preset is made, in the context menu, you can use the command I mentioned above:

    Rename PRESET="My Preset"

    (Where My Preset is your preset name.)

Hi Leo thanks
I have already done that and have the preset in the rename drop down
I am trying to get it on the right click context menu
will have a read of the suggestions thanks

Ok Gents
I have got this far
rename works fine from rename dropdown
added my rename preset to the right click context menu as below

However when it is called it brings up the rename menu to select a preset rather than just run it?

Check that the preset has * for both the Old Name and New Name fields. (i.e. Load it into the Rename dialog. If the fields are different, change them, then re-save the preset.)

If that still isn't working, could you show us a screenshot of the rename dialog with the preset loaded into it and the script editor open (like the screenshot in my post above)?

Hi
preset has * in both mentioned fields
screenshot below

You've saved the preset with the name "My Preset" but in the command you're trying to run a preset called "DO_Rename".

Spot on many thanks Leo, an experienced eye always apreciated