#1 2011-12-29 19:56

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Add leading zeros to pad all numbers the same length

Q: How can i add leading zeros to pad all numbers the same length?
    Dropping the original numbers and use serialize rule to add new numbers is no option
    because the numbers are not in an sequence:

  1.txt    001.txt
  6.txt    006.txt
 10.txt    010.txt
 11.txt    011.txt
 12.txt    012.txt
 99.txt    099.txt
123.txt    123.txt
145.txt    145.txt

A: one way is to use an PascalScript >>> http://www.den4b.com/forum/viewtopic.php?id=26



    But if the names are that simple as with your example
    you can also do this two steps:


    1) Insert: Insert "0000" as Prefix (skip extension)
    Just add as many zeros as you like. Just enough to pad the shortest file name to the wanted length,
    or use many more, it doesn't matter.

    This will get you:

  1.txt      00001.txt
 10.txt     000010.txt
 99.txt     000099.txt
145.txt    0000145.txt

    2) Delete: Delete from Position 4 until the End (right-to-left) (skip extension)
    Use the delete rule and delete all from the 4th position from the right (from the back/from the end) to the begin.

    This will get you:

  1.txt    001.txt
 10.txt    010.txt
 99.txt    099.txt
145.txt    145.txt

Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#2 2011-12-29 21:25

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Add leading zeros to pad all numbers the same length

If there are other signs then just an number in the file name, use an PascalScript like this.

FROM:
Word 1 Word2.txt
Word 4 6.txt
10.txt
Word Word 11 Word.txt
12Word.txt
Word Word 13.txt
Word 20.txt
Word Word Word 21.txt
Word 33Word.txt
Word 99 2011.txt
Word 123.txt
7Word145.txt

TO:
Word 00001 Word2.txt
Word 00004 6.txt
00010.txt
Word Word 00011 Word.txt
00012Word.txt
Word Word 00013.txt
Word 00020.txt
Word Word Word 00021.txt
Word 00033Word.txt
Word 00099 2011.txt
Word 00123.txt
00007Word145.txt

USE:

var
  SubPatterns: TStringsArray;
  SubPatNum1: WideString;

const
  PAD_TO_LENGTH = 5;
  
Begin
   //This RegEx will match first digit in file name, between signs or not:
   SubPatterns:=SubMatchesRegEx(WideExtractBaseName(FileName),'(.*?)(\d+)(.*)',false);
   //If you have to match other digits then just the first found, you have to adjust the regex used.

  //The code.Don't modify:
  
   If Length(SubPatterns) <=0 then exit;
   SubPatNum1 := SubPatterns[1];
   
   while Length(SubPatNum1) < PAD_TO_LENGTH Do
       SubPatNum1 := '0' + SubPatNum1;

   FileName := SubPatterns[0] + SubPatNum1 + SubPatterns[2]
              + WideExtractFileExt(FileName);
End.

Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

Board footer

Powered by FluxBB