#1 2007-05-08 05:50

PauloJ1
Member
Registered: 2007-05-08
Posts: 5

ReNamer - Add leading zeros

It´s a great work. Congratulations.
First I was about to post for a trim function (take off the spaces from the beginning of a folder or file) and then I realize that adding a CleanUp: fix spaces as the last rule solves my problem.

But I have one problem: add leading zeros to existing numbers.

Old file names
pic2.jpg
pic35.jpg
pic4.jpg

New file names (The values of the numbers stay the same)
pic02.jpg
pic04.jpg
pic35.jpg

This example is from 1-4a-Rename that have this function (just check and specify the number of leading zeros).

Two questions:
- Can I do this with PascalScript?
- Is it possible to have this function on future releases?

I'll post it as a sugestion too.

Thanks again.

Offline

#2 2007-05-08 11:10

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: ReNamer - Add leading zeros

Yes, it is possible to do with PascalScript, and the code is below. I will add this as a preloaded script in the new version. Just change the value of PAD_TO_LENGTH constant at the top of the script to whatever length you want all number sequences to be padded to.

const
  PAD_TO_LENGTH = 3;

function CountDigits(const S: WideString; StartI: Integer): Integer;
var I: Integer;
begin
  Result := 0;
  for I:=StartI to WideLength(S) do
  begin
    if IsWideCharDigit(S[i]) then
      Inc(Result)
    else
      Break;
  end;
end;

function MakeZeros(Count: Integer): WideString;
var I: Integer;
begin
  Result := '';
  for I:=1 to Count do
    Result := Result + '0';
end;

var
  Start, Count: Integer;

begin
  Start := 1;
  while Start < WideLength(FileName) do
  begin
    Count := CountDigits(FileName, Start);
    if (Count > 0) then
      if (Count < PAD_TO_LENGTH) then
      begin
        WideInsert(MakeZeros(PAD_TO_LENGTH-Count), FileName, Start);
        Start := Start + PAD_TO_LENGTH;
      end
      else
        Start := Start + Count
    else    
      Inc(Start);
  end;
end.

Offline

#3 2007-05-09 00:06

PauloJ1
Member
Registered: 2007-05-08
Posts: 5

Re: ReNamer - Add leading zeros

OK. That's it.
Thank you.

Offline

#4 2007-05-09 02:07

PauloJ1
Member
Registered: 2007-05-08
Posts: 5

Re: ReNamer - Add leading zeros

I just tried the "serialize duplicates" script and as I like to try things I mixed up the two scripts and that's what pop-up:

const
  PAD_TO_LENGTH = 3;

var
  Files: TStringsArray;

procedure Add(const S: WideString);
begin
  SetLength(Files, Length(Files)+1);
  Files[Length(Files)-1] := S;
end;

function Exists(const S: WideString): Boolean;
var I: Integer;
begin
  Result := False;
  for I:=0 to Length(Files)-1 do
    if WideSameText(Files[i], S) then
      begin Result := True; Break; end;
end;

function MakeZeros(Count: Integer): WideString;
var I, Qtos: Integer;
begin
  Qtos := PAD_TO_LENGTH - WideLength(IntToStr(Count))
  Result := '';
  for I:=1 to Qtos do
    Result := Result + '0';
    Result := Result + IntToStr(Count);

end;

var
  NewFileName: WideString;
  Counter: Integer;

begin
  Counter := 1;
  NewFileName := FileName;
  while Exists(NewFileName) do
    begin
    NewFileName :=
      WideExtractBaseName(FileName) +
      ' ' + MakeZeros(Counter) +
      WideExtractFileExt(FileName);
    Counter := Counter + 1;
    end;
  FileName := NewFileName;
  Add(FileName);
end.

I know that it's not the best and smartter code (have to set PAD_TO_LENGTH to number of leading zeros and Counter as the first number to serialize) , but it works. tongue

If someone could help me to make it better I'll apreciate.

Sorry Denis, but I created a Frankenstein from your 2 codes smile

Offline

#5 2007-05-10 10:32

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,440

Re: ReNamer - Add leading zeros

There is only one issue that I can see, without testing it: It will serialize all files which have a duplicate new name, EVEN if they reside in different folders. This problem has been solved in this topic: rename the same filename but different extension. But be careful, script from that topic ignores the extensions!

Offline

Board footer

Powered by FluxBB