Link to home
Start Free TrialLog in
Avatar of dict
dict

asked on

Search/Replace

Dear friends,

My question is older than running...
I would need your help for writing a script for searching and replace a phrase,
for example, in all files .txt in all disks available on the computer.

Best regards,

Dict
Avatar of mokule
mokule
Flag of Poland image

Avatar of wimmeyvaert
wimmeyvaert

Hi,

Right from the RX-Lib StrUtils-Unit :

function ReplaceStr(const S, Srch, Replace: string): string;
var
  I: Integer;
  Source: string;
begin
  Source := S;
  Result := '';
  repeat
    I := Pos(Srch, Source);
    if I > 0 then begin
      Result := Result + Copy(Source, 1, I - 1) + Replace;
      Source := Copy(Source, I + Length(Srch), MaxInt);
    end
    else Result := Result + Source;
  until I <= 0;
end;

Used in a little application (with a TMemo, 2 TEdits and a TButton) :
When clicked on Button, in the content of the TMemo the text in first TEdit is replaced by the trext in the second one.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text := ReplaceStr( Memo1.Text, Edit1.Text, Edit2.Text);
end;
Hi,

I just made a little demo-application to make use of my previous mentioned function (RX-Lib)
Just put a TButton and 4 TEdits on your form.
Call them :
    Button1: TButton;
    edtPath: TEdit;
    edtFileMask: TEdit;
    edtSearch: TEdit;
    edtRepace: TEdit;


This is a copy of my complete unit.
Just try it out and let me know if this is what you are looking for.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    edtPath: TEdit;
    edtFileMask: TEdit;
    edtSearch: TEdit;
    edtRepace: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure ReplaceTextInFiles( Path, Mask, Search, Replace : String );
    function  ReplaceStr(const S, Srch, Replace: string): string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

Uses FileCtrl;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceTextInFiles(edtPath.Text, edtFileMask.Text, edtSearch.Text, edtRepace.Text );
end;


procedure TForm1.ReplaceTextInFiles(Path, Mask, Search, Replace: String);
var
  sr        : TSearchRec;
  fileAttrs : Integer;
  tstrTmp   : TStringList;
begin
  Try
    fileAttrs := faAnyFile; { parameter to get any file of any type. }
    if not DirectoryExists( Path ) then
      MessageDlg('Directory does not exist : ' + Path, mtError, [mbOK], 0)
    else
    begin
      if not FindFirst( IncludeTrailingBackSlash( Path ) + Mask, fileAttrs, sr ) = 0 then
        MessageDlg('No files found with following mask : ' + Mask, mtError, [mbOK], 0)
      else
      begin
        tstrTmp := TStringList.Create; { to hold the content of the current file }
        Repeat
          tstrTmp.LoadFromFile( sr.Name ); { Load File }
          tstrTmp.Text := ReplaceStr( tstrTmp.Text, Search, Replace ); { do replacements }
          tstrTmp.SavetoFile( sr.Name ); { Save file wit hchanges. }
        until FindNext( sr ) <> 0;
        FindClose( sr );
        FreeAndNil(tstrTmp);
      end;
    end;
  except
    MessageDlg('Something went wrong in replacing the text in the files.', mtError, [mbOK], 0);
  end; { Try Except }
end;

function TForm1.ReplaceStr(const S, Srch, Replace: string): string;
var
  I: Integer;
  Source: string;
begin
  Source := S;
  Result := '';
  repeat
    I := Pos(Srch, Source);
    if I > 0 then begin
      Result := Result + Copy(Source, 1, I - 1) + Replace;
      Source := Copy(Source, I + Length(Srch), MaxInt);
    end
    else Result := Result + Source;
  until I <= 0;
end;

end.




I compiled it with no errors, but have not tested it.
Best regards,

The Mayor.
Found a little bu in my repaut-loop.
It should be :


        Repeat
          tstrTmp.LoadFromFile( IncludeTrailingBackSlash( Path ) +  sr.Name ); { Load File }
          tstrTmp.Text := ReplaceStr( tstrTmp.Text, Search, Replace ); { do replacements }
          tstrTmp.SavetoFile( IncludeTrailingBackSlash( Path ) + sr.Name ); { Save file wit hchanges. }
        until FindNext( sr ) <> 0;
Download from:
http://www.geocities.com/esoftbg/
file:
Q_20932619.zip
It contains: Q_20932619.exe and Q_20932619.txt
Copy Q_20932619.txt into many different folders in all your HDD's
and then start Q_20932619.exe
If you like the exe I'll provide you the source code.

emil
Avatar of dict

ASKER

Dear wimmeyvaert,

I tested your script with success! Thank you very much.

However, your script scan (search/replace) only the files of a directory or disk, not all.

Regards,

Dict
Avatar of dict

ASKER

Dear  esoftbg,

I testes your program with success! Q_20932619.zip

You did a program very useful, congratulations for your ability as programmer!

I am asking by email ´lebronletchev´ the source code.

Thank you

Dict
ASKER CERTIFIED SOLUTION
Avatar of esoftbg
esoftbg
Flag of Bulgaria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dict

ASKER

Dear esoftbg,

Thank you very much. Was possible download your file by my email, yes.

Thank you again.

Dict
Dear dict,
it was very pleasant to help you. I am checking my e-mails rarely and there is always more spam than real expected e-mails, but when I saw ´lebronletchev´ as a sender of an e-mail, I understood what you mean by it.
We can accomplish this question if you accept my comment from 03/28/2004 as a solution with grade A (if you think so).
Thank you,

emil