Search and replace inside files code needed

ST3VO
ST3VO used Ask the Experts™
on
Hi all,

I need a smaple code that will do the following:

I need to search and replace text inside a file between 2 tags.

For example:

Search for: Search for <!start  and <!end and replace anything between them with some specified text specified on a Tmemo.

I don't want to open the file in delphi ... just have delphi search for that and replace it ..then save the changes.

Hope you can help

st3vo


How could I do that please?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
use Pos() for finding locations of your tags
then copy() text from the beginning up to first location, add your replacement and then copy() text from second location up to end.
With tmemo you may easily access entire content as one string.

Author

Commented:
I need a working example please.

thx
Commented:
MyString:=Memo1.Text;
FirstTag:='<start>';
SecondTag:='<end>';
ReplacementString:='New Text';
FirstTagPos:=Pos(FirstTag,MyString);
SecondTagPos:=Pos(SecondTag,MyString);
TempString:='';
TempString:=TempString+Copy(MyString,1,FirstTagPos+length(FirstTag)-1);
TempString:=TempString+ReplacementString;
TempString:=TempString+Copy(MyString,SecondTagPos,MaxInt);

and you have new string in TempString and you can use it.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
Great thank, there's only 1 point that's not being covered here.

TMemo contains the string to replace but I need to specify a file where to FirstTag and SecondTag will be searched.

I need to search in a specified external file.

hope you can help

thanks

st3vo

Commented:
So you have to read entire file (ie with memo.loadfromfile) or with streams, replace and then write new content.
There's no way of doing this without opening the file and reading all the content.
You can also try to use
StringReplace replaces occurrences of the substring specified by OldPattern with the substring specified by NewPattern. StringReplace assumes that the source string may contain Multibyte characters.
 
S is the source string, whose substrings are changed.
 
OldPattern is the substring to locate and replace with NewPattern.
 
NewPattern is the substring to substitute for occurrences of OldPattern.
 
Flags is a set of flags that govern how StringReplace locates and replaces occurrences of OldPattern. If Flags does not include rfReplaceAll, StringReplace only replaces the first occurrence of OldPattern in S. Otherwise, StringReplace replaces all instances of OldPattern with NewPattern. If the Flags parameter includes rfIgnoreCase, The comparison operation is case insensitive.
Geert GOracle dba
Top Expert 2009

Commented:
i know i came across this question once:
it's for large files, but shouldn't be a problem with smaller files either
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23666920.html#22295793

I don't want to open the file in delphi ... just have delphi search for that and replace it ..then save the changes.
Think that "requirement" through.  If you do not open the file, you cannot know what is in it and, therefore, you cannot search for anything in it and, as if that were not enough, howcan you then "save the changes"?
 

Author

Commented:
Hi Geert_Gruwez, tried that code and it's not what I need, looking for something like "windows Grep" or "Actual search and replace" functionwise.

When I said not opening the file I mean't not load into a delphi component at least visibly.

Hope this helps

thanks


When I said not opening the file I mean't not load into a delphi component at least visibly.

In that case, you can load it into a filestream and work with it in memory if there is sufficient memory.  You'll need to read up a bit on FileStreams but you can search them for substrings and do some manipulations on the data.  However, I think that you will probably need to use 2 TFileStream objects so that you can read the file into FileStream1 and then copy selected sections of the data (based upon finding and skipping or substituting what you want to) from FileStream1to FileStream2.  THen you can write out FileStream2 to replace the original file or as a different file.
Geert GOracle dba
Top Expert 2009

Commented:
ST3VO,
easy, just install gexperts into delphi
then you'll have gexperts grep

we can not comply with your requirements
you want to do this in delphi (replace text in a file)
but you do not want to use a delphi component

either you are in the wrong zone (use visual basic for instance)
or figure out your requirements are not possible (i would have said idiotic)

it's like going to the shop and having to come back with groceries, but you're not allowed to go in the grocery store

Author

Commented:
I think I have not explained myself properly.

I want to use delphi for this. The only thing I don't want to do is to open the file into a TMemo and then click on a button to search and replace the text on the memo.

Why? Well, simply because my idea in the future is to select a folder and set a text to search and replace and have it do all the files in the selected folder via a loop.

So, Yes, I want to use delphi but not the wordprocessor search and replace way of doing it.

Hope this helps

thx

st3vo
Geert GOracle dba
Top Expert 2009

Commented:
ahahaaaaa, now we understand
it was a little confusing before your previous remark

i'll post a sample tomorrow (sleep is a essential thing sometimes)
basically
use the PosEx function and remember the result in a private variable
set the SelStart and SelLength change the text with SelText
move the result to the end of the changed text

Author

Commented:
Anyone please?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial