#11 2006-07-07 15:13

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

Re: Question about changing numbers in file names

The reason for all those WideToAnsi and AnsiToWide is simple - there is a nasty bug in the PascalScript implementation in handling WideStrings. I found it out only few days ago... was very disappointed, and wrote to RemObjects but still no reply from them. For some reason it breaks when you try to access characters of a WideString, like WideString[I] (like you do with any string) - it gives a compilation error: Semicolon is missing!?! yikes

FileName variable is a WideString (due to the recently added Unicode support), so I couldn't use FileName[I] to check whenever the character was a digit or not, because of this nasty bug. That's why I had to convert it to an AnsiString (or simply String). In Delphi, WideStrings and Strings (as well as WideChars and Chars) are converted on the fly, when needed. When I used IsWideCharDigit() function, Char from BaseName[I] was converted to a WideChar transparently.

At the end, all this confusion appeared because I couldn't access FileName[I] while FileName been a WideString. I hope this dirty bug in PascalScript will be resolved very soon... smile

Offline

#12 2006-07-26 01:31

dloneranger
Senior Member
From: Birmingham, UK
Registered: 2006-05-03
Posts: 122

Re: Question about changing numbers in file names

Here's a suggested variation that fixes all of the numbers to a fixed length, not just the last one - it's just a slight variation on the code you posted earlier - it could be speeded up quite a lot but I'll leave that as a project (it's fast enough as it is)

in this code files like 'something 1-1.jpg' will be renamed to 'something 001-001.jpg' instead of 'something 1-001.jpg'

var
  BaseName, Extension: string;
  EndDigitsCount, I: Integer;
  NotDigit:boolean;
const
  PAD_TO_LENGTH = 3;

procedure Inc(var A: Integer);
begin
  A := A + 1;
end;

begin
  EndDigitsCount := 0;
  BaseName := WideToAnsi(WideExtractBaseName(FileName));
  Extension := WideToAnsi(WideExtractFileExt(FileName));

  I:=length(BaseName);
  while I>0 do
  begin
      EndDigitsCount := 0;

   NotDigit:=true
   while IsWideCharDigit(BaseName[i]) do
   begin
     EndDigitsCount:=EndDigitsCount+1;
     I:=I-1;
     NotDigit:=false;
   end;
   If NotDigit then
     I:=I-1;

     if (EndDigitsCount > 0) and (EndDigitsCount <= PAD_TO_LENGTH) then
    while EndDigitsCount < PAD_TO_LENGTH do
      begin
      Insert('0', BaseName, I+1);
      EndDigitsCount:=EndDigitsCount+1 ;
      end;
  end;

  FileName := AnsiToWide(BaseName + Extension);

end..

Last edited by dloneranger (2006-07-28 16:21)

Offline

Board footer

Powered by FluxBB