Padding Numbers with Zeros

Would you like Bulk Rename Utility to offer new functionality? Post your comments here!

Padding Numbers with Zeros

Postby DeepBlueSea » Sun Jul 15, 2007 11:54 pm

I have hundreds of folders, each with numbered files (name, space & numbers up to 3 digits, but mostly 2 digits). Numbers 1-9 are one digit & 10-99 two digits & of course 100 and above are three. What I need Bulk Rename to do is to go thru hundreds of files in many different folders and pad the numbers with zero's to make them all the same length. I was able to highlight the files in one folder with numbers 1-9 then have the rename program to add zero's at the correct specified position, but to do that for each folder would be a lot of work. Is it possible to set the program parameters to find any file name with a number and add zero's as needed to create a certain length number? Without the padding some progams that I use will take the files out of sequence, such as 1, 10...19, 2, 20...29, etc. but with the padding the sequence is just fine. I've looked at many renaming programs and could not get any to do the job. I would be thankful for any helpful suggestions. Thanks much!
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Postby Admin » Mon Jul 16, 2007 7:38 am

Why not tick the "use logical sorting" folder option (this mimics XP's sorting) and then remove the existing number and replace it using an autonumber?


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Postby Stefan » Mon Jul 16, 2007 12:18 pm

Right Jim!

Or maybe two times regular expression search and replace?

Lets think what we search for:
A] Search for all from the start ==> (.+)
-- 'A' match all till it find an space followed by an digit followed by the end...that's what we tell it with 'B','C' and 'D'
B] Search for an space ==> \s
C] Search an digit ==> (\d)
D] Search for the end of the string/name ==> $

This give us this RegEx:
Search regEx ==> (.+)\s(\d)$
Replace with ==> \1 0\2

Replace means:
replace with the first ()-group from point A
Then insert an space
Then insert the zero digit 0
Then replace with the second ()-group from point C

Then make an second run but search for two digit with (\d\d)

This is untested but should be OK, so test it first with an copy of your files.

---
Edit:
Replaced my mitake => " first ()-group from C"
with ========= > " second ()-group from C"
Last edited by Stefan on Tue Jul 17, 2007 12:27 pm, edited 1 time in total.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Padding Numbers with Zeros

Postby DeepBlueSea » Tue Jul 17, 2007 4:13 am

Jim,
Thanks for the suggestion. Bulk Rename is the most powerful rename utility that I've come across so far. It has so many features that it will take me a while to figure it all out. I found the "use logical sorting" folder option and checked it off as you suggested, but I'm still trying to understand about removing the existing numbers and I don't know how to use the auto number feature yet. I should have studied the program for a while before asking for help. Sorry about that.
Robert
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Padding Numbers with Zeros

Postby DeepBlueSea » Tue Jul 17, 2007 4:15 am

Stefan,
Thanks for the step by step instructions. It looked a little intimidating at first but I cut & pasted the symbols you listed and it worked with no problems. They work somewhat like the wildcards in MS Word's Find & Replace. Do you know if this will work in a root folder including all the subfolders without having to go into each individual folder? You've been a big help!
Robert
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Postby Admin » Tue Jul 17, 2007 8:12 am

Just tick the "subfolders" option. Then it will work on the current folder and the subfolders.


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Padding Numbers with Zeros

Postby DeepBlueSea » Wed Jul 18, 2007 1:55 am

RegEx:
Search regEx ==> (.+)\s(\d)$ changes Chapter 1.txt
Replace with ==> \1 0\2 to Chapter 01.txt

but what is the search regex to change: 1 Chapter.txt
to: 01 Chapter.txt

I've tries many different things and nothing is working for me. Is there a web page of examples of what others have used this program to do? That would help on things I might want to do in the future.

I have pay pal and would like to make a donation to the author of this program. What info do I need to sent to pay pal besides the email address? Is this the correct email address: "webmaster@ bulkrenameutility.co.uk" for donations?

Thanks!
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Re: Padding Numbers with Zeros

Postby Stefan » Wed Jul 18, 2007 7:32 am

deepbluesea wrote:RegEx:
Search regEx ==> (.+)\s(\d)$ changes Chapter 1.txt
Replace with ==> \1 0\2 to Chapter 01.txt

but what is the search regex to change: 1 Chapter.txt
to: 01 Chapter.txt



An Dot . means find one char, no matter what.
The + sign means find 'one or more' (one or more of any char in this case)
The () means you group that what you have found with your RegEx.

The \s means find an blank (i.e. an "white space" what is more than an blank only)

The \d means find an digit '0' or '1' or '2' .... or '9'
The () means you group that what you have found with your RegEx.

To this groups you can later backreferencing to this group i.e. to them what is in this group.
Do this backreferencing by using \1 for the first group, and use \2 for the second group.


So for
---- 1 Chapter.txt
to: 01 Chapter.txt

dismount the file name:
first you want to find an digit? Use \d ------- better use (\d) for backreferencing later.
Than you have to find an blank with \s
At last you have to find some characters?
There are a few possibilities, but we search simple for any sign with an dot .
We have to catch more then one sign and want to backreferencing later...
... so we use ???? Right! (.+)


So i think we RegEx-search for: (\d)\s(.+)
an digit, an space and as many chars/signs

And then we replace with inserting an zero and backreferencing with: 0\1 \2
an digit 0, the containt of the first group - the old digit, an space inserted by hand, and the containt of the second group -all the other chars/signs



That is an very simple RegEx!!!
There are many possibilities and ways to do this.
Specially if the file name and your rename wish is not that easy.





I've tries many different things and nothing is working for me. Is there a
web page of examples of what others have used this program to do? That
would help on things I might want to do in the future.


For first aid visit Jim's "Regular Expressions" -Forum:
http://www.bulkrenameutility.co.uk/Foru ... um.php?f=3

and find there "Getting Started" with Regular expressions:
http://www.bulkrenameutility.co.uk/Foru ... ic.php?t=5

And maybe visit the links mentioned at:
http://www.bulkrenameutility.co.uk/Foru ... c.php?t=27


I have pay pal and would like to make a donation to the author of this program. What info do I need to sent to pay pal besides the email address? Is this the correct email address: "webmaster@ bulkrenameutility.co.uk" for donations?

Thanks!


Perhaps you will find some infos at:
http://www.bulkrenameutility.co.uk/Donate.php

Good luck!
RegEx and 10 finger tipping will help us an live long :D
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Padding Numbers with Zeros

Postby DeepBlueSea » Wed Jul 18, 2007 9:06 pm

Stefan

RegEx-search for: (\d)\s(.+)
Replace: 0\1 \2

The code you suggested works well until I get to 10 Chapter.txt
for 1 Chapter.txt thru 9 Chapter.txt the code correctly puts 1 zero in front of the number.

But 10 Chapter.txt becomes 00 Chapter.txt and 11 Chapter.txt becomes 01 Chapter.txt

It looks like the padding zero is replacing the 1st digit of the 2 digit numbers. Am I doing something wrong? When I asked for suggestions I forgot to mention that some of the numbers were 2 digit also. Maybe that's what the problem is.
Thanks again, you've been a big help!
Robert
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Re: Padding Numbers with Zeros

Postby Stefan » Thu Jul 19, 2007 7:11 am

DeepBlueSea wrote:Stefan

RegEx-search for: (\d)\s(.+)
Replace: 0\1 \2

But
10 Chapter.txt becomes
00 Chapter.txt and

11 Chapter.txt becomes
01 Chapter.txt


That's what you are searching for whit this RegEx :D
We look for an digit with (\d)
following by an space with \s

So (\d)\s
on
35 Chapter.txt
match the digit 5 and the following space
and you should get
5 Chapter.txt when replacing with '\1 \2'
and
05 Chapter.txt when replacing with '0\1 \2'


You get with this RegEx '(\d)\s' always the digit just before the space.
But not the very first digit as we didn't catch them and so this digit is dropped.

If you then replace with '0\1 \2' you place the new zero digit
on the place of the dropped digit.

Did you read the regex help?
There you see you can search
for one
or more
or even an exactly occurrence of the expression you search for.

Look!
\d search for ONE digit.
\d* search for none or one or two or more digits
\d+ search for --------- one or two or more digits

You want to catch at least ONE digit (names without an starting digit we didn't want to rename, yes?)
But you also look for two or maybe three digits?
So use an expression like \d+

This \d+ will look for one or more digits.

\d => one
\d* => none or many
\d+ => one or many
\d\d => two
\d\d\d => three
\d{3} => three
\d(1,3) => at least one but up to three
\d(,3) => none or up to three


So i guess use
SEARCH: (\d+)\s(.+)
REPLACE: 0\1 \2

You can also experiment wit ^ and $
^ means the beginning of an string (or an name here)
$ means the end of an string

HTH?
If not please ask again.

Greetings to all
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Padding Numbers with Zeros

Postby DeepBlueSea » Thu Jul 19, 2007 4:37 pm

Stefan,

SEARCH: (\d+)\s(.+)
REPLACE: 0\1 \2

This works for 1 digit numbers but for the 2 digit numbers it left me with an unwanted zero at the beginning: "011 Chapter.txt".

So after a little trial & error I came up with:

SEARCH: (^[\d{2}])\s(.+)
REPLACE: 0\1 \2

I'm not sure why, but it seems to skip over the 2 digit numbers and only add a zero the 1 digit ones. This is exactly what I wanted it to do! Now I can get to work on the many hundreds of files that I need to change.

One of the strengths of this program is that I can play around with the code and instantly see the results.

Now I can do what I got this program for. This would have been very difficult to figure out without you pointing me in the right direction. Thank you so much for taking the time to help!!

Robert
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Postby Admin » Thu Jul 19, 2007 7:36 pm

Many thanks Stefan, for helping out here. Much appreciated.


Jim
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: Padding Numbers with Zeros

Postby Stefan » Thu Jul 19, 2007 8:29 pm

DeepBlueSea wrote:Stefan,

SEARCH: (\d+)\s(.+)
REPLACE: 0\1 \2

This works for 1 digit numbers but for the 2 digit numbers it left me with an unwanted zero at the beginning: "011 Chapter.txt".

:D sorry, i provide learning by doing.
Stefan wrote:You can also experiment wit ^ and $


So after a little trial & error I came up with:

SEARCH: (^[\d{2}])\s(.+)

This is no valid RegEx syntax and didn't work in BRU (moment... i have v2.5.3.1 here)
Did this work for you? I think you mixed up smtg by all this trying?

I'm not sure why, but it seems to skip over the 2 digit numbers and only add a zero the 1 digit ones. This is exactly what I wanted it to do!


Aha, you want to prefix
1 chapter
2 chapter
...
9 chapter
only?

I was in mind by your first request/post, i think.

The correct syntax is:

^(\d)\s(.+)

This means
^ ==> search for the beginning
(\d) ==> one digit
\s ==> an white space
(.+) ==> one or many chars
$ ==> maybe it's good to search for the end of the string too


Replace with 0\1 \2


Now I can get to work on the many hundreds of files that I need to change.

One of the strengths of this program is that I can play around with the code and instantly see the results.

Now I can do what I got this program for. This would have been very difficult to figure out without you pointing me in the right direction. Thank you so much for taking the time to help!!

Robert

You are welcome.


- - - - - - -


Admin wrote:Many thanks Stefan, for helping out here. Much appreciated.


Jim


Nothing much, just my kind of donation.
Thank you for BRU!
All the best for you and your family. Good luck!
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Padding Numbers with Zeros

Postby DeepBlueSea » Thu Jul 19, 2007 9:28 pm

Hello Stefan,

I'm using BRU version 2.5.4.3 and "(^[\d{2}])\s(.+)" did work, but I like your code better: "^(\d)\s(.+)" It is less complex and I think that simpler is better. I just tried it and it worked fine.

I'm off to a good start with all you've given me on this subject. I cut and pasted it to a text file so I won't lose it. You're a good (and patient) teacher!

Robert
DeepBlueSea
 
Posts: 8
Joined: Sat Jul 14, 2007 11:54 pm
Location: USA

Postby shhac » Wed Sep 12, 2007 2:28 pm

I know this is abilt late but wouldnt it have been faster to tell him to select all the 1 digit numbers and add the prefix of two 0s, then all the 2 digit numbers and add one 0 prefix?
shhac
 
Posts: 15
Joined: Tue Sep 11, 2007 10:47 pm

Next

Return to Suggestions