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

From den4b Wiki
Jump to navigation Jump to search
Line 22: Line 22:
 
end.
 
end.
 
</nowiki></pre>
 
</nowiki></pre>
 +
This will change a FileName from ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>VIP file.txt<nowiki>’</nowiki>'' which will cause ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' file to be renamed into ''<nowiki>’</nowiki>d:\test\VIP file.txt<nowiki>’</nowiki>''.
  
 
This will change a FileName from ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>VIP file.txt<nowiki>’</nowiki>'' which will cause ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' file to be renamed into ''<nowiki>’</nowiki>d:\test\VIP file.txt<nowiki>’</nowiki>''.
 
  
 
Using the same method (adding a prefix) you might also move a file to a different folder.
 
Using the same method (adding a prefix) you might also move a file to a different folder.
 
 
<pre><nowiki>
 
<pre><nowiki>
 
begin
 
begin
Line 33: Line 31:
 
end.
 
end.
 
</nowiki></pre>
 
</nowiki></pre>
 +
This will change a FileName variable from ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>d:\movetest\file.txt<nowiki>’</nowiki>'' and will cause moving a file ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>d:\movetest\file.txt<nowiki>’</nowiki>''. If the ''<nowiki>’</nowiki>d:\movetest\<nowiki>’</nowiki>'' folder doesn<nowiki>’</nowiki>t exist, it will be created.
  
This will change a FileName variable from ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>d:\movetest\file.txt<nowiki>’</nowiki>'' and will cause moving a file ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>d:\movetest\file.txt<nowiki>’</nowiki>''. If the ''<nowiki>’</nowiki>d:\movetest\<nowiki>’</nowiki>'' folder doesn<nowiki>’</nowiki>t exist, it will be created.
 
  
 
You don't have to specify the explicit path to the file you want to move.
 
You don't have to specify the explicit path to the file you want to move.
 
 
<pre><nowiki>
 
<pre><nowiki>
 
begin
 
begin
Line 43: Line 40:
 
end.
 
end.
 
</nowiki></pre>
 
</nowiki></pre>
 +
This will change the contents of the FileName variable from ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>subfolder\file.txt<nowiki>’</nowiki>'' and will cause moving a file ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' into ''<nowiki>’</nowiki>d:\test\subfolder\file.txt<nowiki>’</nowiki>''.
  
This will change the contents of the FileName variable from ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' to ''<nowiki>’</nowiki>subfolder\file.txt<nowiki>’</nowiki>'' and will cause moving a file ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' into ''<nowiki>’</nowiki>d:\test\subfolder\file.txt<nowiki>’</nowiki>''.
 
  
Let<nowiki>’</nowiki>s stop here and see how does it work.
+
:Let<nowiki>’</nowiki>s stop here and see how does it work.
  
 
After the execution of the script ends, the content of the FileName variable is passed to the NewName field of the files table.
 
After the execution of the script ends, the content of the FileName variable is passed to the NewName field of the files table.
Line 52: Line 49:
 
What happens next has nothing to do with PascalScript. It works for all the rule types.
 
What happens next has nothing to do with PascalScript. It works for all the rule types.
  
If NewName field contains an explicit path (a path that starts from a drive, eg. ''<nowiki>’</nowiki>d:\test\newname.txt<nowiki>’</nowiki>'') it will be copied to the NewPath field.
+
If NewName field contains an '''explicit path''' (a path that starts from a drive, eg. ''<nowiki>’</nowiki>d:\test\newname.txt<nowiki>’</nowiki>'') it will be copied to the NewPath field.
  
If NewName field contains a relative path (eg. ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' or ''<nowiki>’</nowiki>movetest\file.txt<nowiki>’</nowiki>''), NewPath field will be created from the old path of the file and the NewName.
+
If NewName field contains a '''relative path''' (eg. ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' or ''<nowiki>’</nowiki>movetest\file.txt<nowiki>’</nowiki>''), NewPath field will be created from the old path of the file and the NewName.
  
A simple example will make all things clear.
 
  
Let<nowiki>’</nowiki>s say we have a file ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' with Path ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' and our rules produced a NewName <nowiki>’</nowiki>''movetest\file.txt<nowiki>’</nowiki>''.
+
:A simple example will make all things clear.
 +
 
 +
Let<nowiki>’</nowiki>s say we have a file ''<nowiki>’</nowiki>file.txt<nowiki>’</nowiki>'' with Path ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'' and our rules produced a NewName ''<nowiki>’</nowiki>movetest\file.txt<nowiki>’</nowiki>''.
  
 
ReNamer truncates the filename from the end of the Path (now we<nowiki>’</nowiki>ve got ''<nowiki>’</nowiki>d:\test\<nowiki>’</nowiki>'') and adds a NewName to it. So at the end we<nowiki>’</nowiki>ve got NewPath field filled with ''<nowiki>’</nowiki>d:\test\movetest\file.txt<nowiki>’</nowiki>''.
 
ReNamer truncates the filename from the end of the Path (now we<nowiki>’</nowiki>ve got ''<nowiki>’</nowiki>d:\test\<nowiki>’</nowiki>'') and adds a NewName to it. So at the end we<nowiki>’</nowiki>ve got NewPath field filled with ''<nowiki>’</nowiki>d:\test\movetest\file.txt<nowiki>’</nowiki>''.
  
 
When you press Rename button, ReNamer will try to rename files in the files table according to the Path and NewPath fields. It will simply move a file from Path (eg. ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'') to NewPath (eg. ''<nowiki>’</nowiki>d:\test\VIP file.txt<nowiki>’</nowiki>'').
 
When you press Rename button, ReNamer will try to rename files in the files table according to the Path and NewPath fields. It will simply move a file from Path (eg. ''<nowiki>’</nowiki>d:\test\file.txt<nowiki>’</nowiki>'') to NewPath (eg. ''<nowiki>’</nowiki>d:\test\VIP file.txt<nowiki>’</nowiki>'').
 +
  
 
What we need to remember is that NewName (FileName in case of PascalScript) shouldn<nowiki>’</nowiki>t start with a ''<nowiki>’</nowiki>\<nowiki>’</nowiki>'', cause in this case we will get it doubled (eg. ''<nowiki>’</nowiki>d:\test\\movetest\file.txt<nowiki>’</nowiki>'') in the new filename and we<nowiki>’</nowiki>ll get a warning about ''<nowiki>’</nowiki>invalid filename<nowiki>’</nowiki>''.
 
What we need to remember is that NewName (FileName in case of PascalScript) shouldn<nowiki>’</nowiki>t start with a ''<nowiki>’</nowiki>\<nowiki>’</nowiki>'', cause in this case we will get it doubled (eg. ''<nowiki>’</nowiki>d:\test\\movetest\file.txt<nowiki>’</nowiki>'') in the new filename and we<nowiki>’</nowiki>ll get a warning about ''<nowiki>’</nowiki>invalid filename<nowiki>’</nowiki>''.

Revision as of 12:50, 27 May 2009

{{{iparam}}} This article needs to be cleaned up!

FileName variable or How to rename a file

The essence of ReNamer's PascalScript is hidden under a very meaningful name – the FileName variable. It's declaration looks like that:

var FileName: WideString;

which means it is a variable of the WideString type.


A prefix Wide in datatype or function name is used to emphasise that this type/function is capable to work with Unicode characters. FileName variable is the only built-in variable in ReNamer's PascalScript which means that you don’t have to declare it. It's already there. When the execution of the script starts FileName variable contains a name of the currently processed file without a path but with extention (e.g. ’file.txt’).


Renaming of files with PascalScript consist of changing the contents of the FileName variable. So if you want to add "VIP " as a prefix to the present filename you would need such a script:

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

This will change a FileName from ’file.txt’ to ’VIP file.txt’ which will cause ’d:\test\file.txt’ file to be renamed into ’d:\test\VIP file.txt’.


Using the same method (adding a prefix) you might also move a file to a different folder.

begin
  FileName := 'd:\movetest\' + FileName;
end.

This will change a FileName variable from ’file.txt’ to ’d:\movetest\file.txt’ and will cause moving a file ’d:\test\file.txt’ to ’d:\movetest\file.txt’. If the ’d:\movetest\’ folder doesn’t exist, it will be created.


You don't have to specify the explicit path to the file you want to move.

begin
  FileName := 'subfolder\' + FileName;
end.

This will change the contents of the FileName variable from ’file.txt’ to ’subfolder\file.txt’ and will cause moving a file ’d:\test\file.txt’ into ’d:\test\subfolder\file.txt’.


Let’s stop here and see how does it work.

After the execution of the script ends, the content of the FileName variable is passed to the NewName field of the files table.

What happens next has nothing to do with PascalScript. It works for all the rule types.

If NewName field contains an explicit path (a path that starts from a drive, eg. ’d:\test\newname.txt’) it will be copied to the NewPath field.

If NewName field contains a relative path (eg. ’file.txt’ or ’movetest\file.txt’), NewPath field will be created from the old path of the file and the NewName.


A simple example will make all things clear.

Let’s say we have a file ’file.txt’ with Path ’d:\test\file.txt’ and our rules produced a NewName ’movetest\file.txt’.

ReNamer truncates the filename from the end of the Path (now we’ve got ’d:\test\’) and adds a NewName to it. So at the end we’ve got NewPath field filled with ’d:\test\movetest\file.txt’.

When you press Rename button, ReNamer will try to rename files in the files table according to the Path and NewPath fields. It will simply move a file from Path (eg. ’d:\test\file.txt’) to NewPath (eg. ’d:\test\VIP file.txt’).


What we need to remember is that NewName (FileName in case of PascalScript) shouldn’t start with a ’\’, cause in this case we will get it doubled (eg. ’d:\test\\movetest\file.txt’) in the new filename and we’ll get a warning about ’invalid filename’.