Difference between revisions of "ReNamer:Pascal Script:Initialization of variables"
(Created page with '==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 ap...') |
m (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">") |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Up|ReNamer:Pascal Script}} | ||
+ | |||
==Initialization of variables== | ==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. | 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<nowiki>’</nowiki>t simply set starting values | + | 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 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 |
− | < | + | <syntaxhighlight lang="pascal"> |
var | var | ||
− | + | I: Integer; | |
− | |||
begin | begin | ||
− | + | I := 0 | |
− | + | I := I + 1; | |
− | + | FileName := IntToStr(I) + '.txt'; | |
end. | end. | ||
− | </nowiki></ | + | </syntaxhighlight> |
+ | 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<nowiki>’</nowiki>s PascalScript have their default value set to FALSE. With that knowledge we can write initialization part of the script: | ||
+ | |||
+ | <syntaxhighlight lang="pascal"> | ||
+ | var | ||
+ | Initialized: Boolean; | ||
− | + | begin | |
+ | if not Initialized then | ||
+ | begin | ||
+ | //Initialization code here | ||
+ | Initialized:=True; | ||
+ | end; | ||
− | + | // The main renaming code here | |
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | 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. |
+ | <syntaxhighlight lang="pascal"> | ||
var | var | ||
Initialized: Boolean; | Initialized: Boolean; | ||
Line 34: | Line 53: | ||
// The main renaming code here | // The main renaming code here | ||
end. | end. | ||
− | </ | + | </syntaxhighlight> |
− | + | 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. | Now we can rewrite the serialization script from previous chapter so it could start eg. from 5. | ||
− | < | + | <syntaxhighlight lang="pascal"> |
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; | ||
+ | I := I + 1; | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
− | + | [[Category:ReNamer]] | |
− | + | [[Category:Pascal Script]] | |
− | |||
− |
Latest revision as of 15:03, 8 February 2017
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.