Difference between revisions of "ReNamer:Pascal Script:FileName Utilities"

From den4b Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Up|ReNamer:Pascal Script}}
 +
 
==Basic FileName Utilities or How to skip extention==
 
==Basic FileName Utilities or How to skip extention==
  
 
A FileName variable is a string, so we can do with it anything that we could do with a string.
 
A FileName variable is a string, so we can do with it anything that we could do with a string.
  
We already know how to concatenate (add together) strings. We did that in the previous chapter, when we were adding a prefix to a FileName. Let<nowiki></nowiki>s try to add a postfix now.
+
We already know how to concatenate (add together) strings. We did that in the previous chapter, when we were adding a prefix to a FileName. Let's try to add a postfix now. As before we will use a constant string ''<nowiki>'</nowiki>_part2<nowiki>'</nowiki>'' (constant strings in Pascal need to be enclosed in apostrophes) and concatenation operator (<nowiki>+</nowiki>).
  
As before we will use a constant string ''<nowiki>’</nowiki>_part2<nowiki>’</nowiki>'' (constant strings in Pascal need to be enclosed in apostrophes) and concatenation operator (<nowiki>+</nowiki>).
+
<syntaxhighlight lang="pascal">
 
 
<pre><nowiki>
 
 
begin
 
begin
FileName:=FileName + '_part2';
+
  FileName := FileName + '_part2';
 
end.
 
end.
</nowiki></pre>
+
</syntaxhighlight>
  
What we<nowiki>’</nowiki>ve got is ''<nowiki>’</nowiki>file.txt_part2<nowiki>’</nowiki>'' and that<nowiki>’</nowiki>s probably not what we expected, but it<nowiki>’</nowiki>s purely logical. We added together ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' and ''<nowiki>’</nowiki>_part2<nowiki>’</nowiki>''. The result is correct.
+
What we've got is "''file.txt_part2''" and that's probably not what we expected, but it's purely logical. We added together "''file.txt''" and "''_part2''". The result is correct. So what should we do when we want to add something after the filename but before the extention? There is no "skip extention" option for PascalScript rules. The answer is simple: we need to split the FileName into base filename and extention and insert "''_part2''" in between.
  
So what we should do when we want to add something after the filename but before the extention. There is no <nowiki>’</nowiki>skip extention<nowiki>’</nowiki> option for PascalScript rules.
+
At that point ReNamer comes in hand with its standard procedures and functions. We will take a look on two main functions that you can find among FileName Utilities: '''WideExtractBaseName''' and '''WideExtractFileExt''' functions. I guess their names are clear enough. So let's write our code.
  
The answer is simple: we need to split the FileName into base filename and extention and insert ''<nowiki>’</nowiki>_part2<nowiki>’</nowiki>'' in between.
+
<syntaxhighlight lang="pascal">
 
 
At that point ReNamer comes in hand with its standard procedures and functions. We will take a look on two main functions that you can find among FileName Utilities: WideExtractBaseName and WideExtractFileExt functions. I guess their names are clear enough. So let<nowiki>’</nowiki>s write our code.
 
 
 
<pre><nowiki>
 
 
begin
 
begin
FileName:=WideExtractBaseName(FileName)+ '_part2'+ WideExtractFileExt(FileName);
+
  FileName := WideExtractBaseName(FileName) + '_part2' + WideExtractFileExt(FileName);
 
end.
 
end.
</nowiki></pre>
+
</syntaxhighlight>
 +
 
 +
And that's it. The result for "''file.txt''" is ''<nowiki>'</nowiki>file_part2.txt<nowiki>’</nowiki>''.
  
And that<nowiki>’</nowiki>s it. The result for ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' is ''<nowiki>’</nowiki>file_part2.txt<nowiki>’</nowiki>''.
+
[[Category:ReNamer]]
 +
[[Category:Pascal Script]]

Latest revision as of 15:05, 8 February 2017

Basic FileName Utilities or How to skip extention

A FileName variable is a string, so we can do with it anything that we could do with a string.

We already know how to concatenate (add together) strings. We did that in the previous chapter, when we were adding a prefix to a FileName. Let's try to add a postfix now. As before we will use a constant string '_part2' (constant strings in Pascal need to be enclosed in apostrophes) and concatenation operator (+).

begin
  FileName := FileName + '_part2';
end.

What we've got is "file.txt_part2" and that's probably not what we expected, but it's purely logical. We added together "file.txt" and "_part2". The result is correct. So what should we do when we want to add something after the filename but before the extention? There is no "skip extention" option for PascalScript rules. The answer is simple: we need to split the FileName into base filename and extention and insert "_part2" in between.

At that point ReNamer comes in hand with its standard procedures and functions. We will take a look on two main functions that you can find among FileName Utilities: WideExtractBaseName and WideExtractFileExt functions. I guess their names are clear enough. So let's write our code.

begin
  FileName := WideExtractBaseName(FileName) + '_part2' + WideExtractFileExt(FileName);
end.

And that's it. The result for "file.txt" is 'file_part2.txt’.