Topic: Change length of # at end of filename

The help file gives the following example:
From :                 To:
Img1.jpg             Img_001.jpg
Img2.jpg             Img_002.jpg
Img10.jpg           Img_010.jpg

Use the expression :
Img_%N.%e or Img_%N{3}.%e

The following expression will work with any filename which has # at end of the name:
%b(s/(\d+$)//i)%N{3,,-1}.%e

Example:
anyfilename-2.jpg                =>  anyfilename-002.jpg
Any.filename34.jpg              =>  Any.filename034.jpg
Any17_filename_12.jpg       =>  Any17_filename_012.jpg

Obviously use any desired values in the %N expression.

To add a separator, put desired character between the RegEx  & the %N:
%b(s/(\d+$)//i)_%N{3,,-1}.%e
%b(s/(\d+$)//i)-%N{3,,-1}.%e

To remove a separator, put undesired separator before the \d:
%b(s/(_\d+$)//i)%N{3,,-1}.%e
%b(s/(-\d+$)//i)%N{3,,-1}.%e

To change the separator, do both (note: there is a space between _ and ~)
%b(s/([-_ ~.]\d+$)//i)_%N{4,,-1}.%e
ie:
Filename-1.jpg         =>   Filename_0001.jpg
Filename_20.jpg       =>  Filename_0020.jpg
Filename 300.jpg      =>  Filename_0300.jpg
Filename~4000.jpg   =>  Filename_4000.jpg
Filename.050.jpg      =>  Filename_0050.jpg

2 (edited by vipiish 2014-04-04 13:23:07)

Re: Change length of # at end of filename

Funny that this thread is at the top! I came here today wondering if there was a way to do pretty much this.. Except in my case, I'd like to do it on two [b]hex[/b] numbers.

I got a bunch of files that ends with offset numbers in hex, like this:
0x0-0xa0800.dat
0xa0800-0x110000.dat
0x110000-0x1b2800.dat
...

I would have liked to rename them like so:
0x00000000-0x000a0800.dat
0x000a0800-0x00110000.dat
0x00110000-0x001b2800.dat
...

I tried this (which I thought would work, but.. I guess not):
%b(s/(.+?)0x([0-9a-f]+)\-0x([0-9a-f]+)/\10x\2{8}-\3{8}/)

But I guess you can't use Siren modifiers inside the regex replacement format, huh? Also, how do I escape literal numbers right after group references in the replacement format (like that "\10x", which should be interpreted as "\1" -> "0x", and not "\10" -> "x")?

(I had to put the text inside code tags because it stripped EOLs for some reason..)

Re: Change length of # at end of filename

Welcome to this forum vipiish,

You are right variable/modifier can't be embedded in regular expressions.
The "\10" case you report looks like an issue. I'll try to find an answer to it.

For the renaming you are trying to achieve I am not sure that a regular expression is the most appropriate.
Here is an expression that should do it (supposing that the hexa numbers are separated by a '-' and are the last part of the base name) :
%b(s/0x.*$//)0x%b[-2]("x","x0000000")(-8)-0x%b[-1]("x","x0000000")(-8).%e

Lets have a look at its different parts :
%b(s/0x.*$//) : extracts the text before the first hexadecimal number
0x%b[-2]("x","x0000000")(-8) : pads the first hexadecimal number to height digits
0x%b[-1]("x","x0000000")(-8) : pads the second hexadecimal number to height digits
.%e : adds the dot and the extension of the file

The "tricky" part is the padding. Lets detail the first one using the file name : File0xa0800-0x110000.dat

%b[-2] : will extract the last but one string supposing they are separated by '-'. In this case : File0xa0800
%b[-2]("x","x0000000") : will insert seven '0' just after the 'x'. In this case : File0x0000000a0800
%b[-2]("x","x0000000")(-8) : will extract the last height characters (the hexa number left paded). In this case : 000a0800
0x%b[-2]("x","x0000000")(-8) : will recreate the expected hexa number. In this case : 0x000a0800

I hope this will help,

Regards,

Remi

Re: Change length of # at end of filename

Hohoh, good one! I already found a different way to do what I wanted (wrote a script), but I'll remember this for later. And thanks for the quick response. Siren is awesome, keep it up, Rémi. smile