ReNamer:Pascal Script:FileName

From den4b Wiki
Jump to: navigation, search

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").

Add a prefix to a filename

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".

Move file to absolute folder

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.

Move file to relative folder

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\movetest\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".