Rename to 'file' of 'total files'

Is there an elegant way to rename a series of files using sequential numbering AND adding the total filecount to the end of the filename?




test-1 of 3.mpg
test-2 of 3.mpg
test-3 of 3.mpg

I was kind of surprised that there doesn't seem to be a way to do this (well, a way I could find anyway :slight_smile: )
The filecount stuff either relates to the status bar, which is no good and the metadata filecount apparently isn't supposed to work or maybe be there in this context (as Leo states in an earier post).

On a related note, I was experimenting with a dirty kind of two pass way to do it and saw that if you add the ADVANCED argument to..
"Rename PATTERN * TO [#]* NUMBER"

the command no longer works. What I mean is that sans the ADVANCED argument it works as expected, but as soon as you add the ADVANCED argument, when the Advanced Rename dialog opens, the NUMBER argument seems to be ignored (until you manually check it anyway). Is this intentional or am I missing something?

Rename metadata is confined to details of the individual file being renamed, so the total number of files isn't directly available.

It's fairly easy to do with a script, though, if you don't mind running it via a button or hotkey from outside of the Rename dialog. I can write that for you. Do you just want it to add " of X" before the file extension, where X is the total, or to add "-X of Y" before it where X is a count? How much control do you want over the count (e.g. starting number, step size, zero padding)?

Hey Leo,

Yeah, I figured there was no direct way to do this and as you know, scripting isn't my forte. The X part I thought could come from the sequential numbering function, and the of Y part from...?

Don't see a need for it to be harder than it has to be, so assume the starting number to always be 1, zero padding needed, though it will never exceed three digits total count. The padding part is almost always going to be just one zero, since the file count will rarely exceed 99 files, so is there a way to use the 'right' amount of padding automatically? In other words, not just add say two leading zeroes even though the file total is only say 12? Yes, the "of Y" part will be before the extension. Step will always be 1.

It needs to be flexible enough to be able to do both your scenarios. In other words, IF the files already have numbers, just add the "of Y" to the end of the file name, but it also needs the flexibilty of being able to also add the count if needed, AS WELL as rename the files on the fly.

To be specific, I am talking about renaming audiobook mp3s. The file names come in all manner of formats, so this is why the rename dialog is needed, generally, though not always, to build the filename from scratch using the sequential function. eg.

OLD: *
NEW: author - book title .mp3

With sequential numbering turned on to add . I was kinda hoping for a command to add so the rename would look like

OLD: *
NEW: author - book title of .mp3

Another scenario is where the files are named reasonably, but incompletely, eg., rather than "author - book title num", they are simply "book title num" and in this case I use a preset of

OLD: *
NEW: -*

which means I don't have to type the whole thing, since the book title and numbering is already done.

Basically, I need the flexibility, without needing to input too much stuff, which kind of defeats the purpose.

Can't I just have a placeholder? lol

Anyway, that is the story, so have at it.

Thanks Leo

I once did a very specific rename prototype of script to pick up on the numbering a set of files already has (and if not start at 1) and use the first selected file to fetch the filename for all other selected files. It is a bit difficult to explain and I also wasn't very creative finding a name for the script command. That thing and some background info, usage descriptions can be found here.

Now I had a quick look at it again and by tweaking two lines it seems this modded version below, get's quite close. But you cannot really customize the pattern used for the final names without editing the code. I thought, maybe you can live with that or let's have that for further discussion.

Just select a bunch of files and run "RenameBaseNameContinueNumbering" from button or command field.

Here it goes:
Command.File_RenameBaseNameContinueNumbering.js.txt (3.97 KB)

ps: You need to enable the script command first and also try with no number in your filenames, could end up in better result.

hmm, doesn't seem to do anything? Made a button, set it to script function, added the text, and... nothing happens, whether or not the files have numbers in them or not.

I don't get why this is so hard, it isn't as if Opus doesn't know how many files there are. It has status bar codes to do this so why not others?

Anyway, unless I am missing something obvious that you can think of, it doesn't seem to work at all?

thanks anyway :slight_smile:

You need to put the file into "/dopusdata/script addins" then enable the script and thereby the contained command in Preferences -> toolbars -> scripts -> [X] Command.File: RenameBaseNameContinueNumbering.

Then just use the new command called "RenameBaseNameContinueNumbering" in a regular "standard funcion" button.
Phew! o)

I knew that.
Ah, so this isn't like Opus v3?

My bad entirely, sorry. Ok, did all that, and it still does nothing. I assume I did it correctly because the new command is in the COMMANDS list now.

No it's not like in DO v3.. you crazy! o)

Did you select files before running the command?

Yup, select, click, flash, nuttin (files deselect).

Time for bed in the land down under, so don't sweat this one now mate. Thanks though. I am sure we'll get there in the end one way or another.


Maybe open the "other logs" or script console output panel and see wether there are any error messages.
For me it works with this bunch of testfiles:

Invalid character (0x800a0408)
Parse error - script aborted
Error at line 1, position 1
Type mismatch: 'RenameBaseNameContinueNumbering' (0x800a000d)
Error at line 1, position 1

... a lot more of these

Type mismatch: 'RenameBaseNameContinueNumbering' (0x800a000d)
Error at line 1, position 1
Type mismatch: 'RenameBaseNameContinueNumbering' (0x800a000d)

I have to go to bed before it's time to get up!

The quest continues...

It sounds like you've set your button that runs the command as a script function. You should change it to a normal command button (since it's just invoking a command as normal - even though the command itself happens to have been added by a script).

Hey Jon :slight_smile:

Nah, I thought of that and tried it as a script when the normal button didn't work, even though it isn't a script in the normal sense, which is something I should have realised in the first place.

Since you are visiting, :slight_smile: why doesn't


work? If you take away the ADVANCED arg, it works fine, but if you run it as is, the [#] only works AFTER you check the sequential numbering.

Maybe you also like to try this new approach:
To get around the unavailability of a placeholder, I had the idea to get it by scripting and then run the regular rename command in REGEXP and NUMBER mode to achieve automatic numbering and injecting the total count value at once.

It even works - after some heavy fails! o)

[code]@script jscript

function OnClick(data){
var count = data.func.sourcetab.selected.count;
var cmd = data.func.command; cmd.deselect = false;
cmd.RunCommand('Rename PATTERN="(.)\.(.)" TO="\1 [#] of '+count+'.\2" REGEXP NUMBER');

What about setting the button to script automatically if there is a @script modifier?
I notice quite some users not aware of the button types and even me is forgetting about it at times.

Reading Leos first post here again, my idea wasn't so new I guess. o))

So we wait for Leo then? (or a new command from Jon maybe? :stuck_out_tongue:)

I'd still like to know why the script won't run. For that matter, neither will the second one, though I admit I am not certain how this is supposed to work. No matter, I am sure all will become clear, one way or another.

You might say that since in many cases I am renaming the files from scratch, why not just add the 'of num' manually?
The answer is that in the case of the manual rename, it really isn't a big ask, but my request initially was how to add just the 'of num' to the end of files that already are numbered correctly, without needing to resort to manually inputting the number via a {Rs}. Since it would be handy though to do both, well...

Biggus, you once managed to get the JoinMP3 script command running, so the skill is yours basically! o))

For the *.js.txt script add-in featuring the "RenameBasenameContinueNumbering" command, the button needs to look like this:

For the inline script from my last post, the button needs to be setup like this:

Use the inline script and remove the "NUMBERED" switch as well as the "[#]" placeholder from the rename command line.

Another one! o)

This one adds number if not already present, else re-numberes and always adds or updates "of ". Good? o)

Filename.txt         -->  Filename 1 of 4.txt
Filename 2.txt       -->  Filename 2 of 4.txt
Filename 22.txt      -->  Filename 3 of 4.txt
Filename 2 of 3.txt  -->  Filename 4 of 4.txt

[code]@script jscript

function OnClick(data){
var count = data.func.sourcetab.selected.count;
var cmd = data.func.command; cmd.deselect = false;
cmd.RunCommand('Rename PATTERN="(.?)( \d+(?: of \d+)?)?($|\..)" TO="\1 [#] of '+count+'\3" REGEXP NUMBER');

Yup, got that. I needed a 'refresher' on the js thing, it is a new fangled idea that didn't exist back in the day, and I have only ever used it once before, so I kinda forgot how.

The script thing though I have plenty of, I know how to do that one.

The thing is the first one does not work, despite being set as your grabs indicate. This is the crux of the biscuit.
The second one (the script button, as opposed to the RenameBase etc command one) also did not work, but now mysteriously does?
I don't mind holding my hand up to a mistake, but I changed nothing apart from clearing the script log? Now the script works :slight_smile:

How to modify it to add zero padding? (Enough zeroes to match the total num, rather than a fixed number of zeroes. As I said, the total file count will rarely exceed two digits, but could run to three, so one or two zeros would suffice, though no padding of course with a single digit total.

Use the inline script and remove the "NUMBERED" switch as well as the "[#]" placeholder from the rename command line.[/quote]

Yup, I know this, I should have been more exacting in what I wrote. What I should have said was...

"my initial request was how to add just the 'of num' to the end of the files, using an external control code, or some other existing Opus function."