ReNamer:Pascal Script:Initialization of variables

From den4b Wiki
Revision as of 16:03, 8 February 2017 by Den4b (talk | contribs) (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Initialization of variables

When ReNamer invokes the script?

One of the important things to understand when you play with ReNamer is that rules are applied file-by-file, which means you get all the rules applied to the first file and then all the rules (in order as they appear in rules window) applied to the next file and so on.

For PascalScript rules it means that script is run for x times (where x is number of files marked for renaming in the files table). The trick that makes script useful is that it preserves values of variables between executions of the script during one Preview operation (although variables are reset to their default values every time you press Preview or AutoPreview takes place). It means you can rely on any incrementation you let the script to do. The drawback of that situation is that you can’t simply set starting values for your variables in the script, because they will be reset to these starting values on every execution of the script (so for every file), eg. this script

var
  I: Integer;
begin
  I := 0
  I := I + 1;
  FileName := IntToStr(I) + '.txt';
end.

will make every filename to be "1.txt".

How to initialize variables?

If we need any initialization of variables we need an ability to write such a code that will be executed only during the first execution of the script. To do that we have to learn one very useful thing: all boolean variables in ReNamer’s PascalScript have their default value set to FALSE. With that knowledge we can write initialization part of the script:

var
  Initialized: Boolean;

begin
  if not Initialized then 
  begin
    //Initialization code here
    Initialized:=True;
  end;

  // The main renaming code here
end.

The initialization code is executed only if Initialized variable is FALSE. And setting the Initialized variable to True at the very end of the initialization code prevents the initialization code from being executed more than once.

More elegant way of doing the same thing is invoking the Initialize procedure.

var
  Initialized: Boolean;

procedure Initialize;
begin
  Initialized := True;
  // Initialization code here
end;

begin
  if not Initialized then Initialize;
  // The main renaming code here
end.

Rule remains the same. The nice thing about this way is that the initialization code is neatly separated from the main code of the script.

How to serialize files starting from any given number?

Now we can rewrite the serialization script from previous chapter so it could start eg. from 5.

var
  Initialized: Boolean;
  I: Integer;

procedure Initialize;
begin
  Initialized := True;
  I := 5;
end;

begin
  if not Initialized then Initialize;
  FileName := IntToStr(I) + '_' + FileName;
  I := I + 1;
end.