Number formatting / padding (file name)

Bulk Rename Utility How-To's

Number formatting / padding (file name)

Postby John in NZ » Wed Jun 21, 2023 7:22 am

Hi folks,

sorry for the dumb noob question, but how do I re-format numbers in a file name?

1.txt to 001.txt
9.txt to 009.txt
10.txt to 010.txt
99.txt to 099.txt

I know this has to be ridiculously easy, but I can't find the answer in the pdf manual

TIA

John
John in NZ
 
Posts: 3
Joined: Wed Jun 21, 2023 7:15 am

Re: Number formatting

Postby Admin » Wed Jun 21, 2023 12:49 pm

With Bulk Rename Javascript in Special (14) :

Code: Select all
function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

newName = pad(name, 3);
Admin
Site Admin
 
Posts: 2354
Joined: Tue Mar 08, 2005 8:39 pm

Re: Number formatting / padding (file name)

Postby therube » Wed Jun 21, 2023 7:53 pm

(Request for enhancement: Pad / Fill a numeric (or otherwise) value.)
therube
 
Posts: 1319
Joined: Mon Jan 18, 2016 6:23 pm

Re: Number formatting / padding (file name)

Postby therube » Wed Jun 21, 2023 8:14 pm

If... & assuming...
If your files are "ordered" correctly, or if ordering does not matter, you can do something like, & assuming all you were wanting was a set of numerically increment file names...


2:Name, Remove

10:Numbering
Mode: Suffix, Start:1, Pad:3


That would give you a numerically ordered, padded, set of filenames.

What it would not do is if you had 1.txt 2.txt 4.txt,
it would give you 001.txt 002.txt 003.txt (with 003.txt, rather then [just] padding 4 to 004.txt).
therube
 
Posts: 1319
Joined: Mon Jan 18, 2016 6:23 pm

Re: Number formatting

Postby John in NZ » Thu Jun 22, 2023 1:11 am

Admin wrote:With Bulk Rename Javascript in Special (14) :

Code: Select all
function pad(n, width, z) {
  z = z || '0';
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

newName = pad(name, 3);

Thanks for answering my question, and seeing that you need to dig into java to answer the question makes me feel better about needing to answer the question in the first place.

However, it also surprises me that padding numbers to the same number of digits isn't available from the default selection boxes, given that Windows can have issues with sequencing file names numerically or literally and having numbers padded to the same length avoids these issues ... :-)

Again, thanks ... 8)
John in NZ
 
Posts: 3
Joined: Wed Jun 21, 2023 7:15 am

Re: Number formatting / padding (file name)

Postby John in NZ » Thu Jun 22, 2023 1:19 am

therube wrote:If... & assuming...

We all know what assumptions are don't we, the mother of all ... I'm sure you know the rest ... ;)

More seriously, yes the files are named sequentially, and correctly, but some folders have the files named without any leading zeros on the one and two digit numbers (generally there are up to 200 files in the folders), and imo that just looks messy, and yes, I do have bigger things to worry about than tidy file names ... :lol:
John in NZ
 
Posts: 3
Joined: Wed Jun 21, 2023 7:15 am

Pad numbers to 3-digits

Postby Luuk » Thu Jun 22, 2023 4:24 am

Therube is just making sure that no numbers might be missing, so then Numbering(10) would properly pad the numbered-filenames.
If numbers can be missing and not having the paid version, can modify... http://www.bulkrenameutility.co.uk/foru ... 100#p16913
Its for padding them to 12-digits, so would just change 12--->3.
Luuk
 
Posts: 706
Joined: Fri Feb 21, 2020 10:58 pm

Re: Number formatting / padding (file name)

Postby Admin » Fri Jun 23, 2023 7:31 am

Could have a simple control to PAD name if the name is just a number, but what if the name is name1.txt and needs to be name0001.txt.
Mayne the best option would be to add a pad function to RegEx.
Admin
Site Admin
 
Posts: 2354
Joined: Tue Mar 08, 2005 8:39 pm


Return to How-To