Rename Regular Expressions Help

Hi, sorry this is probably really basic but I'm trying to learn.

I have a group of files named with the pattern * - [*] * where the asterisks are different words and other characters. I am trying to delete everything but the last string, which is essentially everything after the ] and before the file extension. I thought the simplest solution would be
Old: ( . * ) ] ( . * )
New: \2
Ignoring extension

But this does nothing. I thought that (.*) means a string of any length of any characters and \2 refers to the second string. Can you explain what I am missing and what the easiest solution would be?

Also is there an easy way to affix a random unique number with a specific amount of padding to the front of a file as a match rename process?

Thanks!

You need to escape the ] character with a \ .
Also be careful of space characters.

Edit Note:
The timing of these two posts were really almost simultaneous

1 Like

After some trial and error, it seems like any brackets or parentheses in the original filename will break the regular expressions. Is there a quick fix for this?

I had to add spaces in my example in order to avoid italics.

Ok adding a \ before the bracket worked, thanks. I still don't fully understand the why lol.

I guess because it parses as regular expression syntax.
It goes right to left, so it is expecting a [ character .

1 Like

I'm getting the hang of it thanks! Lol I even found the backtick key on Gboard finally (clearly I am a not from this world)

Is there an easy way to append a random number padded to 4 digits in a batch file rename process?

I'm USA.
Obviously, I'm not from this world.
But, I've been reading, learning and posting here for some time.
I think it has done me some good.
Locals just don't get it !

My homemade pizza is almost done.
I'll have to think on that question, but I know I have a rename preset script that does random characters FWIW.

The more concise way would be by using evaluator I think, but I've never tested it in such context.
Otherwise a rename script can do that but will require a litlle more coding.

This should work
RENAME PATTERN "*" TO "*_{=d=(Floor(10000*Rnd()) as int) as "%04"; return d;=}" IGNOREEXT

If you want the number appended without the _ before it, remove it in the TO part (TO "*{=d=(Floor(10000*Rnd()) as int) as "%04"; return d;=}"

Unfortunately, Rnd() will return the same for all selected files.

1 Like

Argh, I only tested on one file :frowning:
Then it will have to be scripted I guess

EDIT : You can try this script button.

function OnClick(clickData)
{
	var cmd = DOpus.Create.Command();
	cmd.deselect = false; // Prevent automatic deselection

	// --------------------------------------------------------
	DOpus.Output("Selected items in " + clickData.func.sourcetab.path + ":");
	if (clickData.func.sourcetab.selected_files.count == 0)
	{
		DOpus.Output("No file selected. Nothing to rename.");
		return;
	}

	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext()) {
		var file = eSel.item();
		var action = 'RENAME "' + file.realpath + '" TO "' + file.path + '\\' + file.name_stem + pad(Math.floor(Math.random() * 10000), 4) + file.ext + '"'; 
		cmd.AddLine(action);
	}

	cmd.Run();
}

function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

Add Random Number.dcf (1.9 KB)

1 Like

to get you started - needs tweaking to get it to do what you want.

Random numerals rename.orp (463 Bytes)

An .orp file can be imported into the advanced rename dialog

Yes.
Renames can use column keywords, but I have been unable to use Evaluator Column Keywords.

Do you happen remember my homemade Random Number Evaluator Column that I posted when you requested the Rnd() Evaluator function ?
Evaluator Function: Random Number Generator

My column, admittedly a bit crude, does result in different random numbers for all files/folders in the column.

If we could access Evaluator Column keywords in rename, it may be possible to do this using Evaluator. The trouble in this example is my use of Global Variables. That admittedly could be a problem.

Here it is again in XML.

<?xml version="1.0"?>
<evalcolumn align="0" attrrefresh="no" autorefresh="no" customgrouping="no" foldertype="all" header="Random" keyword="RandomNumber" maxstars="5" namerefresh="no" reversesort="no" title="Random Number" type="0">m=Pow(2,16) + 1;
a=75;
c=74;
if ( $glob:seed)
   {
   x=$glob:seed;
   }
   else
   {
   $glob:seed=1;
   x=1;
   }
x=(a*x + c)Mod m;
$glob:seed=x;
return x;
</evalcolumn>

We can :slight_smile:

The column could be slightly modified, i.e. replace

return x;

with

return Left(x as "%04", 4);

and use it like this:

Rename PATTERN=* TO={eval:RandomNumber} IGNOREEXT AUTONUMBER

(I would still prefer an Evaluator random function that generates a new number every time it is called. So far, I haven’t encountered a scenario where the current behavior has been useful.)

1 Like

I think it does that already. The problem is it’s only being called once to generate the command line. The rename command doesn’t see the evaluator code because it has already been turned into a string during argument parsing.

Escaping the {=…=} code is needed, so it is parsed by the Rename command instead of being parsed before the command runs. I think that’s done by doubling the first { but I am not currently at a PC to verify that.

Escaping doesn't help here. Even when used directly in the Renamer, Rnd() will only generate one value:

Interestingly, when the files are actually renamed the second half receives a new random number:


Yes, it is. E.g.

Rename PATTERN=* TO="* {{=Left(target, 4)=}" IGNOREEXT AUTONUMBER
1 Like

@lxp Thankyou !

I love this button, how would it be modified to use sequential numbers instead of random numbers like: file 01.jpg, file 02.jpg, file 03..jpg.

Numbering Files

We'll fix that in the next update.

Without space with numbers on 4 digits:

RENAME NUMBER 0001

With space but I don't know how to handle padding :

Rename PATTERN * TO "* [#]" NUMBER

With space and padding, using button script :

function OnClick(clickData)
{
	var cmd = DOpus.Create.Command();
	cmd.deselect = false; // Prevent automatic deselection

	// --------------------------------------------------------
	if (clickData.func.sourcetab.selected_files.count == 0)
	{
		DOpus.Output("No file selected. Nothing to rename.");
		return;
	}

	var i = 1;
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected_files); !eSel.atEnd(); eSel.moveNext()) {
		var file = eSel.item();
		var action = 'RENAME "' + file.realpath + '" TO "' + file.path + '\\' + file.name_stem + " " + pad(i++, 4) + file.ext + '"'; 
		cmd.AddLine(action);
	}

	cmd.Run();
}

function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

Add Incrementing Number.dcf (1.8 KB)

EDIT : If you want to change the padding size in the script, you need to change the 4 in pad(i++, 4)