#1 2007-01-26 11:05

pstein
Member
Registered: 2006-10-28
Posts: 43

New function: Replacing text string INSIDE multiple files ?

ReNamer is a nice tools.

However it is only a small step to add a new, very useful function:

I often want to replace text strings inside a couple of files e.g.

I want to replace all text pattern

"aaa bbb"

by

"ccc 1$2"

inside all files with pattern *.html
recursively in the directory tree starting with

D:\project\test23

Could you include such a function in future versions of Renamer ?

Thank you

Peter

Offline

#2 2007-01-26 22:49

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

Re: New function: Replacing text string INSIDE multiple files ?

Peter,

If I understood correctly, you want to actually change the contents of files, is that right? ReNamer already has this functionality (using PascalScript rule), BUT it is still highly un-recommended, because all actions will be done on preview without any feedback to the user, and without any room for mistakes. ReNamer is not designed for editing contents of files, it is designed for renaming files, perhaps using the file contents in renaming operation.

If you want to do that despite my warning, you will need to write a script for that. Functions which might help you: ReplaceRegEx, WideReplaceText, WideReplaceStr, FileReadContent, FileWriteContent. I can help you writing the script.

Offline

#3 2007-01-27 11:52

pstein
Member
Registered: 2006-10-28
Posts: 43

Re: New function: Replacing text string INSIDE multiple files ?

Missing preview is acceptable for me as long as the replacement function is reliable and working.

Maybe a list function for all replacements (after finish) would be fine

However writing a script is very unhandy. I need a comfortable function.

Why not introducing 4 entry fields:

1.) filename patterns  (e.g.all "*.txt")
2.) Starting directory e.g. D:\myproject\test1\
     Maybe an additional checkbox: recursive=yes/no
3.) "From" pattern (text string to be replaced)
4.) "To" pattern (new text string)

That would be perfect for me.

Offline

#4 2007-01-27 12:55

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

Re: New function: Replacing text string INSIDE multiple files ?

Script will give you all that functionality which you want. For example:

var
  Ext: WideString;
  Contents: string;
begin
  Ext := WideExtractFileExt(FilePath);
  if not WideSameText(Ext, '.html') then Exit;
  Contents := FileReadContent(FilePath);
  FileWriteContent(FilePath+'.backup', Contents);
  Contents := WideReplaceText(Contents, 'aaa', 'bbb');
  FileWriteContent(FilePath, Contents);
end.

This script will only process "*.html" files, it will first read their contents, create a backup file with a ".backup" extension, will replace all "aaa" with "bbb", and then will write changed contents back to the file. WideReplaceText() functions works in case-insensitive mode, for case-sensitive replace you need WideReplaceStr(). If you want to use regular expressions, then use ReplaceRegEx function (look into help for syntax). WARNING: TEST REPLACE PATTERNS BEFORE USING ON REAL FILES!

You can set default directory for ReNamer by creating a shortcut with command line (dont forget the inverted commas):
"C:\Program Files\ReNamer\ReNamer.exe" "D:\myproject\test1"

Recursive or not recursive option is available from within settings.

Look, there are programs which are designed to do that, i.e. replace text/patterns within files. Have a look at this topic: Text Search & Replace Program using similar rules as Renamer.

Offline

Board footer

Powered by FluxBB