MRT Question: adding padding/leading zeros
Moderators: Hacker, petermad, Stefan2, white
-
- 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
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
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
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.
But there might be some problems if something contains a digit followed by an underscore.
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
Padding digits with zero's to an given length
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.
(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.
Re: MRT Question: adding padding/leading zeros
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.
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.
2seb-
You can also use:
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
-
- Senior Member
- Posts: 276
- Joined: 2011-11-15, 06:14 UTC
- Location: DE\BN - only part time TC user after switching to Linux ;)
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
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
- Balderstrom
- Power Member
- Posts: 2148
- Joined: 2005-10-11, 10:10 UTC
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.
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.
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
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.