MRT Question: adding padding/leading zeros

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
seb-
Senior Member
Senior Member
Posts: 276
Joined: 2011-11-15, 06:14 UTC
Location: DE\BN - only part time TC user after switching to Linux ;)

MRT Question: adding padding/leading zeros

Post by *seb- »

Hi there...

i have a folder with files like

File_1_something.txt
File_2_something.txt
File_300_something.txt
File_30_something.txt
File_3_something.txt

So there is a text, then an underscore (_) and then a number and again underscore and some text, then extension.

The texts at the beginning and end might be as well separated by underscores into more parts (e.g. a_b_1_c_d.ext)

I wanted to use the MRT to add leading zeros to the files with only one number, to achieve the following


File_001_something.txt
File_002_something.txt
File_003_something.txt
File_030_something.txt
File_300_something.txt

so that the files are really in the correct order.

How to do it... i tried with my limited RegExp experience, but i wasn't successful...


thanks for any suggestions

br
seb
User avatar
MVV
Power Member
Power Member
Posts: 8711
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

I think the easiest way is to do it in 3 steps, one step for 1-digit numbers, other one for 2-digit numbers and third one for 3-digit numbers.

Code: Select all

Find: (([^_]*_)+)(\d)(_.*)
Replace with: $1\0\0$3$4

Code: Select all

Find: (([^_]*_)+)(\d\d)(_.*)
Replace with: $1\0$3$4

Code: Select all

Find: (([^_]*_)+)(\d\d\d)(_.*)
Replace with: $1$3$4
But there might be some problems if something contains a digit followed by an underscore.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Padding digits with zero's to an given length

Post by *Stefan2 »

Or in two steps only.

(i have not tested the general regex, only the digit part)

Step 1 - adding as many zero's as maximal needed (or more) to the found digits:

([^_]*_)+(\d+_.*)
$1\00000$2

Results in:
File_000001_something.txt
File_000002_something.txt
File_000003_something.txt
File_0000030_something.txt
File_00000300_something.txt


Then Step 2 - find none-or-more digits, with trailing three digits to keep:

([^_]*_)+(\d*)(\d\d\d_.*)
$1$3

Results in:
File_001_something.txt
File_002_something.txt
File_003_something.txt
File_030_something.txt
File_300_something.txt


Just an idea.
User avatar
nsp
Power Member
Power Member
Posts: 1956
Joined: 2005-12-04, 08:39 UTC
Location: Lyon (FRANCE)
Contact:

Re: MRT Question: adding padding/leading zeros

Post by *nsp »

Jut an addition to Stefan2 solution, the two steps can be chained using F5 key (Reload result)

search expression could be ([^_]*_)+(0*)(\d{3}_.*)
1) only trigger 0 characters instead of any digit
2) \d\d\d can be shortened with \d{3} (exactly 3 digits.
seb-
Senior Member
Senior Member
Posts: 276
Joined: 2011-11-15, 06:14 UTC
Location: DE\BN - only part time TC user after switching to Linux ;)

Post by *seb- »

Do i need to check or type something else. Trying MMVs or Stefan2s solution does nothing...

brs
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Checking [X] RegEx check box?

Just try it yourself! There are not that many options to check.
seb-
Senior Member
Senior Member
Posts: 276
Joined: 2011-11-15, 06:14 UTC
Location: DE\BN - only part time TC user after switching to Linux ;)

Post by *seb- »

User avatar
Hacker
Moderator
Moderator
Posts: 13144
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Post by *Hacker »

seb-,
Make sure there is no space at the end of your "Search for" line.

Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Post by *Stefan2 »

Looks perfect, till it does nothing for you.

It seams you have an additional space somewhere, perhaps an trailing one in the search field?
User avatar
white
Power Member
Power Member
Posts: 6017
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Post by *white »

2seb-

You can also use:

Code: Select all

Rename mask: file name:   00[N]

Search for:               ^(00)(.*_)(\d_)|^0(0)(.*_)(\d{2}_)|^00
Replace with:             $2$1$3$5$4$6

RegEx: checked
Subst: not checked
seb-
Senior Member
Senior Member
Posts: 276
Joined: 2011-11-15, 06:14 UTC
Location: DE\BN - only part time TC user after switching to Linux ;)

Post by *seb- »

Thanks for the hint with the space...

It seems that altough i try to select text here as precisely as possible.. somehow theres an additional space around it...

Okay, now i know for the future to keep an eye on superfluous whitespaces and to have [N] and [E] set as well in the left part... ;)

Thank you all!

br Seb
User avatar
Balderstrom
Power Member
Power Member
Posts: 2148
Joined: 2005-10-11, 10:10 UTC

Post by *Balderstrom »

I have a set of regex's that do that within TC - in one step, except they can't handle an indeterminate amount of text after the index number.

The easiest way I would do that, would be to utilize TC's MRT, and the Editor option to edit file names. Then as my editor - that would be EmEditor --- which allows you to do multi-step regex'es in one pass, and save those "Batch Replaces" as a named TSV file.

E.g. if you replace all of the "|" below with tabs, it will be a valid TSV that emeditor could load to mass rename any number of files.
1) You use EmEditor to be the default from TC's MRT/Edit Names.
2) Run/Load the Batch-Regex within EmEditor
3) Save the file.
4) Go back to TC, click OK.

Code: Select all

on|_|Ѫ|R
on|Ѫ(\d+)|_\1|R
on|_(\d)Ѫ|_00\1Ѫ|R
on|_(\d{2})Ѫ|_0\1Ѫ|R
on|Ѫ|_|R
The first step is to replace all of the underscores with a unique - unused character, it doesn't matter what it is, I chose Ѫ
Then change Ѫ and a number to underscore and a number.
Then do your 0 insertions in 2 steps.
Then replace any Ѫ with underscores again.

With more complex regex's you could likely reduce those steps by 1 or 2 -- but with EmEditor's batch-regex, it allows you to write more maintainable _simpler_ regexes that you can have more control over and will be less likely to break when encountering something that you didn't plan for or expect.
Post Reply