Difference between revisions of "ReNamer:Pascal Script:Initialization of variables"
(source tag and code syntax changes) |
|||
Line 5: | Line 5: | ||
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<nowiki>’</nowiki>t simply set starting values of 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 | 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<nowiki>’</nowiki>t simply set starting values of 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 | ||
− | < | + | <source> |
var | var | ||
− | + | I: Integer; | |
− | |||
begin | begin | ||
− | + | I := 0 | |
− | + | I := I + 1; | |
− | + | FileName := IntToStr(I) + '.txt'; | |
end. | end. | ||
− | </ | + | </source> |
will make every filename to be "1.txt". | will make every filename to be "1.txt". | ||
Line 20: | Line 19: | ||
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<nowiki>’</nowiki>s PascalScript have their default value set to FALSE. With that knowledge we can write initialization part of the script: | 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<nowiki>’</nowiki>s PascalScript have their default value set to FALSE. With that knowledge we can write initialization part of the script: | ||
− | < | + | <source> |
var | var | ||
Initialized: Boolean; | Initialized: Boolean; | ||
Line 34: | Line 33: | ||
// The main renaming code here | // The main renaming code here | ||
end. | end. | ||
− | </ | + | </source> |
Procedure Initialize is executed only if Initialized variable is FALSE. And as the procedure sets the variable Initialized to TRUE, it prevents the initialization code to be executed more than once. | Procedure Initialize is executed only if Initialized variable is FALSE. And as the procedure sets the variable Initialized to TRUE, it prevents the initialization code to be executed more than once. | ||
Line 40: | Line 39: | ||
Now we can rewrite the serialization script from previous chapter so it could start eg. from 5. | Now we can rewrite the serialization script from previous chapter so it could start eg. from 5. | ||
− | < | + | <source> |
var | var | ||
Initialized: Boolean; | Initialized: Boolean; | ||
− | + | I: Integer; | |
procedure Initialize; | procedure Initialize; | ||
begin | begin | ||
Initialized := True; | Initialized := True; | ||
− | + | I := 5; | |
end; | end; | ||
begin | begin | ||
if not Initialized then Initialize; | if not Initialized then Initialize; | ||
− | + | FileName := IntToStr(I) + '_' + FileName; | |
− | FileName:=IntToStr( | + | I := I + 1; |
− | |||
end. | end. | ||
− | </ | + | </source> |
Revision as of 02:09, 3 August 2009
Initialization of variables
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 of 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".
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;
procedure Initialize;
begin
Initialized := True;
// Initialization code here
end;
begin
if not Initialized then Initialize;
// The main renaming code here
end.
Procedure Initialize is executed only if Initialized variable is FALSE. And as the procedure sets the variable Initialized to TRUE, it prevents the initialization code to be executed more than once.
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.