Difference between revisions of "ReNamer:Pascal Script:Read file content"

From den4b Wiki
Jump to navigation Jump to search
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{Up|ReNamer:Pascal Script}}
 +
 +
There are several [[ReNamer:Pascal_Script:Functions|functions]] available which make this task simple, for example: '''FileReadContent''', '''FileReadLine''' and '''FileCountLines'''. Below are several examples of how to use these functions.
 +
 +
<span style="color: red">'''Warning:'''</span> Some functions are able to alter your file system, so use those with caution! Also, remember that processing large files in the script can dramatically slow down renaming process.
 +
 +
== Read first few characters ==
 +
 +
Here is how you can insert into the filename first few characters of file content.
 +
 +
<syntaxhighlight lang="pascal">
 +
begin
 +
  FileName := FileReadFragment(FilePath, 0, 20) + ' ' + FileName;
 +
end.
 +
</syntaxhighlight>
 +
 +
== Find and extract specific portion ==
 +
 +
Imagine that you are processing large number HTML files which have ban names. You want to insert the HTML Title into the name for files, i.e. the content of the <nowiki><title></nowiki> tag. This would require reading the file, finding the title tag, and extracting the text inside the tag. Below is a simple example:
 +
 +
<syntaxhighlight lang="pascal">
 +
var
 +
  Text, Title: String;
 +
  TitleStart, TitleEnd: Integer;
 +
begin
 +
  Text := FileReadContent(FilePath);
 +
  TitleStart := Pos('<title>', Text);
 +
  TitleEnd := Pos('</title>', Text);
 +
  if (TitleStart > 0) and (TitleEnd > 0) then
 +
  begin
 +
    TitleStart := TitleStart + Length('<title>');
 +
    Title := Copy(Text, TitleStart, TitleEnd-TitleStart);
 +
    FileName := Title + ' ' + FileName;
 +
  end;
 +
end.
 +
</syntaxhighlight>
 +
 +
Note: The script is a simple demonstration and in real cases might not work for all of the files, for example if title tag is written as "<TITLE>" instead of "<title>".
 +
 
== Read lines from file ==
 
== Read lines from file ==
 
There are couple of [[ReNamer:Pascal_Script:Functions|functions]] available which make this task simple: '''FileReadLine''' and '''FileCountLines'''.
 
  
 
For example, if you have a text file containing names (one name per line) which you would like to assign to a list of files, you can use the script below:
 
For example, if you have a text file containing names (one name per line) which you would like to assign to a list of files, you can use the script below:
  
<pre>
+
<syntaxhighlight lang="pascal">
 
var
 
var
 
   I: Integer;
 
   I: Integer;
Line 12: Line 49:
 
   FileName := FileReadLine('C:\Names.txt', I);
 
   FileName := FileReadLine('C:\Names.txt', I);
 
end.
 
end.
</pre>
+
</syntaxhighlight>
 +
 
 +
[[Category:ReNamer]]
 +
[[Category:Pascal Script]]

Latest revision as of 15:01, 8 February 2017

There are several functions available which make this task simple, for example: FileReadContent, FileReadLine and FileCountLines. Below are several examples of how to use these functions.

Warning: Some functions are able to alter your file system, so use those with caution! Also, remember that processing large files in the script can dramatically slow down renaming process.

Read first few characters

Here is how you can insert into the filename first few characters of file content.

begin
  FileName := FileReadFragment(FilePath, 0, 20) + ' ' + FileName;
end.

Find and extract specific portion

Imagine that you are processing large number HTML files which have ban names. You want to insert the HTML Title into the name for files, i.e. the content of the <title> tag. This would require reading the file, finding the title tag, and extracting the text inside the tag. Below is a simple example:

var
  Text, Title: String;
  TitleStart, TitleEnd: Integer;
begin
  Text := FileReadContent(FilePath);
  TitleStart := Pos('<title>', Text);
  TitleEnd := Pos('</title>', Text);
  if (TitleStart > 0) and (TitleEnd > 0) then
  begin
    TitleStart := TitleStart + Length('<title>');
    Title := Copy(Text, TitleStart, TitleEnd-TitleStart);
    FileName := Title + ' ' + FileName;
  end;
end.

Note: The script is a simple demonstration and in real cases might not work for all of the files, for example if title tag is written as "<TITLE>" instead of "<title>".

Read lines from file

For example, if you have a text file containing names (one name per line) which you would like to assign to a list of files, you can use the script below:

var
  I: Integer;
begin
  I := I + 1;
  FileName := FileReadLine('C:\Names.txt', I);
end.