How do I shorten names?

Is there any way to have Opus shorten both directory and filenames in a selected list so that they are no longer than a set number of characters of my choosing?

Many thanks,
Kirk

You should be able to do that, yes.

How do you want it to shorten the names which are too long? Just remove the characters after that point (preserving the file extension)? Or something more complex?

Is it just name length you care about, or total path length?

I'd like to keep the filename extension, but, say, clip the filename length to an arbitrary number based upon my need is at any given time. I would also like to shorten directory names in the same fashion - not the ENTIRE path, such as C:\programs\etc... but, just the directory level that I am currently selecting. For example, if a directory as 45 characters in its name, I'd like to shorten the directory length to 30, without it affecting any of the directory structure below and above that level of directory nesting (I hope that makes sense!).

Starting with a simple (but probably not quite right) example, this shortens things to 30 characters while ignoring the extensions (and the length of the extensions; more on that below):

Mode: regular expressions
Old name: ^(.{30}).*
New name: \1

Some of those names are longer than 30 characters due to the extensions on the end, which weren't counted. If you know the extensions are always 4 characters then you could just use 26 as the limit, but that's probably not ideal.

You probably want to include the length of the extensions in the calculation automatically.

I'm guessing you also don't want to have any spaces before the extensions as well, as that looks a bit weird.

In that case, let's switch to using a rename script.

As a bonus, this script also gives you a field you can input your maximum length into, without having to worry about regular expressions and so on:

image

Now the results should be what you actually want:

Here's the rename preset, which can be used in the Rename Dialog as shown above, or in toolbar buttons, hotkeys, etc. if you want to automate things further (shout if you need help with those):


For reference, this is the rename script, which is inside that preset. You'll see it if you click "Edit" in the Rename dialog, next to where it says "Script defined" in bold.

You don't need to understand this script code to use it, but it shows you how things work if you want to do something similar in the future.

function OnGetCustomFields(getFieldData)
{
	getFieldData.fields.maxLength = 30;
	getFieldData.field_labels("maxLength") = "Maximum Length";
}

// This is the renaming function
function OnGetNewName(getNewNameData)
{
	var stem = getNewNameData.newname_stem_m
	var ext = getNewNameData.newname_ext_m

	var lenMax = getNewNameData.custom.maxLength;
	var lenTotal = stem.length + ext.length;

	if (lenTotal > lenMax)
	{
		var lenNewStem = stem.length - (lenTotal - lenMax);
		DOpus.Output(lenNewStem);

		if (lenNewStem > 0)
		{
			stem = stem.substr(0, lenNewStem);
			// Trim spaces from the end.
			stem = stem.replace(/\s+$/, '');

			return stem + ext;
		}		
	}

	return;
}

That looks great, but, I am unsure of how to add this script to Dopus. Can you walk me through that? I open the rename dialog, but, I can't figure out how to make it add new items to the left side window where those predefined scripts are.

Never mind...I found the import option. Thanks for all the help, Leo!

It's in the guide I linked if you still need help.