#1 2006-07-06 21:34

johnnyrobot
Member
Registered: 2006-07-06
Posts: 1

Reading text from a file

Is there a way to read text from the the contents of a file, then grab a string from the file, then use that string in renaming of the file?

Say that 'article_Title_3.15.2005.txt' contains the string '<title>Renamer Tool Speeds Productivity</title>' somewhere in the file.

Could I write a script that would search the file for that (title) string, then rename the file to 'article_'Renamer Tool Speeds Productivity_3.15.2005.txt'?

Hope this makes sense. Thanks in advance. The software is of great help for me!

Offline

#2 2006-07-07 14:52

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,374

Re: Reading text from a file

Yes, it is possible! Are you familiar with PascalScript rule or Delphi/Pascal programming language at all?

If not, then describe all the variations of your files and what exactly you want to read from them and where exactly you want to paste whatever you read from them. Give as much description as you can, preferably with examples. I can write a script for you smile

Offline

#3 2006-07-22 19:47

luminos
Member
Registered: 2006-07-22
Posts: 1

Re: Reading text from a file

Hi Denis!


Fantastic program!!


I also need this same functionality, I need to search a text file for a string and then rename the file using that string.

I have some programming experience, but am unfamiliar with the syntax of PascalScript, is there any way you could post an example of how to do this?


Many thanks in advance!

Keep up the good work

Offline

#4 2006-07-22 21:03

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,374

Re: Reading text from a file

Hi luminos, and thanks smile

There are sample scripts in the PascalScript rule configurations, find a button called "Scripts", and in the dropdown menu you'll get few examples, including the one to read a text file line-by-line. The syntax of the PascalScript is the same as Pascal or Delphi. Just search the net for Delphi codes and examples, you'll get looooods of results. And here is a good site to start from http://www.delphibasics.co.uk/.

If you are stack, just tell me what you are trying to do (with examples), and I'll write a script for you!

Offline

#5 2007-07-06 14:07

Circumflex
Member
Registered: 2007-07-06
Posts: 2

Re: Reading text from a file

Hi den4b. This programm is a masterpiece.

I have the same question: need to do this

1. Get the list of selected (for renaming) filenames.
2. Write this list into array.
3. Read the first line from every file in queue into some variable, then use this variable as this file's name.

Like this:

begin
  I := I + 1;
  FileName := FileReadLine ( FileNamesArray[i], 1);
end.

I don't know how to put list of selected files into array.
And I'm not very familiar with the PascalScript, sorry roll

English isn't my native language (which is Russian) so excuse me for it ^_^

Last edited by Circumflex (2007-07-06 14:08)

Offline

#6 2007-07-06 14:18

Circumflex
Member
Registered: 2007-07-06
Posts: 2

Re: Reading text from a file

OMG, it's so easy and I've found it in help yikes

var
  I: Integer;
  NewName: WideString;
begin
  I := I+1;
  NewName := FileReadLine(FilePath, 1);
  FileName := NewName;
end.

Thanks anyway, I've renamed ALL of my downloaded from lib.ru book collection.
Excellent work!

Offline

#7 2007-07-06 16:33

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,374

Re: Reading text from a file

You actually need only that much:

begin
  FileName := FileReadLine(FilePath, 1);
end.

By the way, Im Russian too! big_smile

Offline

#8 2007-11-02 03:01

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Reading text from a file

Circumflex wrote:

...
3. Read the first line from every file in queue into some variable, then use this variable as this file's name.
...

My first pascal script.

I have tried to solve this challenge too:

// take a line from every file 'Name' and use this as 'New Name' for this same file
// f.ex.: file 'Name' "some.txt" contain line "Version 1.2" and so 'New Name' will be "Version 1.2.txt"
// 01 Nov. 2007 by Stefan

const
  LineNumber = 1; //which line to choose from file content? The first line? The second? Or the third?
  NewNameLenght = 20; // how long should the 'New Name' be? Choose between 1 and 100 only. This line has f.ex. 122 chars.
  InValidCharsReplacement = ''; //replace invalid chars in 'New Name' with this char For nothing use ''
  NoContentFoundString = 'No Content Found'; // like to know that no content is found? For nothing use ''
  
var
  OldName : WideString;
  OldExt : String;
  NewName : WideString;
  count : Integer;

    
begin
//Show an message only once:
If count < 1 Then
ShowMessage('Please exclude binary files from extracting lines!'+#13#10+'Otherwise you could get messy file names.')
Count := 1

  //store the original file name
  OldName := WideExtractBaseName(FileName)
  //store the extension of the file name
  OldExt := WideExtractFileExt(FileName)

  // catch the line number x from the file 'Name'
  NewName := FileReadLine(FilePath, LineNumber);
  
  // clean up the NewName by replacing invalid chars  \ / : * ? " < > | 
  NewName := WideReplaceStr(NewName, '\', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '/', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, ':', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '*', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '?', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '"', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '<', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '>', InValidCharsReplacement);
  NewName := WideReplaceStr(NewName, '|', InValidCharsReplacement);

  //shorten the 'NewName' if needed
  If Length(NewName) > NewNameLenght then
     SetLength(NewName,NewNameLenght);
  
  //do you want to know that there is no content found? Then the 'New Name' will become an message:
  If Length(NewName) < 1 Then
  NewName := NoContentFoundString
  
  /////////////////////////////////////////////////////
  // Generate the output 'New Name' as you want him.
  // Uncomment only one of the following examples:
  
  // Set 'New Name' to    "Line.ext"
  FileName := NewName + OldExt
  
  //  Set 'New Name' to   "OldName (Line).ext"
  //FileName := OldName + ' (' + NewName + ')' + OldExt
  
  //  Set 'New Name' to   "OldName - Line.ext"
  //FileName := OldName + ' - ' + NewName + OldExt
  
  //  Set 'New Name' to   "Line [OldName].ext"
  //FileName := NewName + ' [' + OldName + ']' + OldExt

end.

Hope this is from some use for someone.

Thanks to Denis for an great tool!


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#9 2007-11-03 14:59

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,374

Re: Reading text from a file

Looks like a lot of code for what it does wink

I would use a simpler script (below), and to strip the invalid characters I would use standard rules.

Of course, I understand that your script is much more flexible, but mine will be much faster for what it does big_smile

var
  Line: WideString;

begin
  Line := FileReadLine(FilePath, 1);
  if Line <> '' then
    FileName := Line + WideExtractFileExt(FileName);
end.

Offline

Board footer

Powered by FluxBB