Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search and replace in a file 1

Status
Not open for further replies.

vertygo

Programmer
Oct 3, 2004
2
CA
How would I go about doing a search and replace in a file.

For example, changing all the 1's to 0's

I can open, append, close and work with files OK, but I don't know how to write a search and replace.

Thanks!
 
Personally, I use a very quick method:
1) Drop a TRichEdit component on your form (found in the Win32 tab)
1) Load a text file into the RichEdit component using RichEdit1.Lines.LoadFromFile('...');
2) Drop a TReplaceDialog on the form and in its' OnReplace event place the following code:
Code:
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var
  SelPos: Integer;
begin
  with TReplaceDialog(Sender) do
  begin
  { Perform a global case-sensitive search for FindText in Memo1 }
    SelPos := Pos(FindText, RichEdit1.Lines.Text);
    if SelPos > 0 then
    begin
      RichEdit1.SelStart := SelPos - 1;
      RichEdit1.SelLength := Length(FindText);
      { Replace selected text with ReplaceText }
      RichEdit1.SelText := ReplaceText;
    end
    else
      MessageDlg(Concat('Could not find "', FindText, '" in RichEdit1.'), mtError, [mbOk], 0);
  end;
end;
3) Drop a TButton on the form and label it "Replace"
and in its' OnClick event place the following code:
Code:
procedure TForm1.Button2Click(Sender: TObject);
begin
  ReplaceDialog1.Execute; 
end;

This will serve as a starting point for you. It might be nice to also have a "load" button linked to a TOpenDialog so that the user can navigate to and select a file. It all depends on the application you're creating. The Delphi help file does have lots of examples for the various dialogs.


Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Sorry, no forms in this app, should have mentioned it
 
You don't say how you're loading the files. I like to use a TStringList for it rather than using the File I/O routines. It would look something like this:
Code:
  slFile := TStringList.create;
  slFile.LoadFromFile('C:\MyFile.txt');
  for i := 0 to (slFile.count - 1) do
    slFile[i] := StringReplace(slFile[i], '1', '0', [rfReplaceAll]);
  slFile.SaveToFile('C:\MyFile.txt');

-Dell
 
It is not necessary to reiterate through all the lines of the file. It is a good idea to use a try...finally block to ensure allocated resources are released.
Code:
slFile := TStringList.Create;
try
 slFile.LoadFromFile ( 'c:\myfile.txt' );
 slFile.Text := StringReplace ( slFile.Text, '1', '0', [rfReplaceAll] );
 slFile.SaveToFile ( 'c:\myfile.txt' );
finally
 slFile.Free;
end;




Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top