Solved

replacing text within a text file

Posted on 2002-07-02
8
179 Views
Last Modified: 2013-11-18
hi everyone,

is there a way to open a text file, search for a text, and replace all that with an empty string?

for example i have the text in a file:

hello
hello world what's up
bye

i want to search for "hello world" and replace it with "blank/empty string" or delere that text from the file so at the end i'll have

hello
what's up
bye

thanks
0
Comment
Question by:urif
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7124551
This is very easy - there are lots of ways.
One of the easiest is using TStringList to grab file in
memory, and then using StringReplace procedure to replace
your text.
If you wish, I'll post you example that does this.
0
 

Author Comment

by:urif
ID: 7124574
thx, please if you can post an example.
0
 

Author Comment

by:urif
ID: 7124576
oh, i forgot to mention, i'll be using this in a console application, so a lot of the vcl components maybe are not available.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 7

Accepted Solution

by:
Cynna earned 50 total points
ID: 7124611
You'll need TStringList, which uses Classes, and StringReplace (defined in Sysutils). I presume this won't be a problem for console app.

Copy/Paste:
----------------

function FileTextReplace(FileName, OriginalString, ReplacementString: String): Boolean;
var i: Integer;
    s: String;
begin
 Result:=FALSE;
 with TStringList.Create do begin
     try
         if not FileExists(FileName) then Exit;
         // Grab file into TStringList:
         LoadFromFile(FileName);
         // Copy file contents to single string variable:
         s:=Text;
         // Replace string (look at help for StringReplace() function, for further info):
         s:=StringReplace(s, OriginalString, ReplacementString, [rfIgnoreCase]);
         // Copy modified file back to TStringList:
         Text:=s;
         // And finally save it to same file:
         SaveToFile(FileName);
         Result:=TRUE;
     finally
        Free;
     end;
 end;
end;


DEMO:
-------
(place Button1 on Form1)

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Delete 'hello world' from C:\bla.txt:
  if FileTextReplace('C:\bla.txt', 'hello world', '') then ShowMessage('Replaced OK.')
                                                      else  ShowMessage('Error!');
end;
0
 

Author Comment

by:urif
ID: 7124720
i refrace the question and let me know if i should put it as a different one, so you can get the points:


the function works fine, i modified it a bit to suit my program. the problem is that one of the strings i need to replace is random, meaning, it could be anything, it's not a fixed string. what i know is that it is enclosed like in HTML and XML inside <> and that the open tag is <name> and the close is </name>

so basically i need find the opening tag and delete the string beginning (and including) the open tag all the way up to the close tag. example:

line 1 some text
line 2<mytag>some random string</mytag>
line 3 some other text

i need to delete the whole thing at the end i'll have

line 1 some text
line 2
line 3 some other text
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7124854
urif,

Oh well...this is a bit more complicated then what you
initially stated.
I can write you a function that will accept input MyTag,
and delete everything in between (including tags), like
you specified.

To be clear, take few examples:

a) file1.txt looks like this:

line 1 some text
line 2<title>some random string</title>
line 3 some other text

b) file2.txt looks like this:

line 1 some text
line 2<title>some random string</title>
line 3<bla>text</bla> text2 <blabla> text3 <blabla>


Lets take our function declaration for example:
 CutTags(FileName, TagName: String)

Then, a call like this:

a)  CutTags('file1.txt', 'title') would change file1.txt to this:
line 1 some text
line 2
line 3 some other text

b)  CutTags('file2.txt', 'bla') would change file1.txt to this:

line 1 some text
line 2<title>some random string</title>
line 3 text2 <blabla> text3 <blabla>


If you can wait a day or two, I'll write this for 50 points.
If you want it ASAP, you'll get it in about 1 hour for 100 points.

0
 

Author Comment

by:urif
ID: 7124875
it's ok thx so much!

i'll figure it out.

actually i placed another question before placing this one here and i think it was answered.

again, thanks.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7124900
No problem, glad I could help....
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select distinct 25 100
Delphi: ForceDirectory plain function 7 31
Where do I learn ES6? 3 68
Installshield for Embarcadero EX 10.1 Berlin 4 41
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question