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:

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:

To remove a separator, put undesired separator before the \d:

To change the separator, do both (note: there is a space between _ and ~)
%b(s/([-_ ~.]\d+$)//i)_%N{4,,-1}.%e
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:

I would have liked to rename them like so:

I tried this (which I thought would work, but.. I guess not):

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) :

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,



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