Link to home
Start Free TrialLog in
Avatar of Zip58
Zip58

asked on

multi-file search and replace

Hello

Can somebody help me with some sourse code or a link where I can find a example with source

ho to make a file Grep for multi-file search and replace in multi directories

Thanks

Regards

Zip58
Avatar of lmikle
lmikle

Simple find files and process them. Use FindFirst/FindNext/FindClose functions to find files.

// this procedure process single file
procedure ProcessFile(FileName, SearchStr, ReplaceStr : String);
begin
...
end;

// this procedure recieve a list of folders name
// that must be processed and start processing of them.
// Folders name is the full folder name
// with the ending slash: "C:\MyDocs\" for example
procedure ProcessFolders(FldrList : TStringList; SearchStr, ReplaceStr : String);
var
  I : Integer;
begin
  For I := 0 To FldrList.Count-1 Do
    ProcessFiles(FldrList, SearchStr, ReplaceStr);
end;

procedure ProcessFiles(FolderName, SearchStr, ReplaceStr : String);
var
  iFound : Integer;
  SR : TSearchRec;
begin
  // Find all files and folders
  iFound = FindFirst(FolderName+'*.*',faAnyFile,SR);
  While iFound = 0 Do
    Begin
      if (SR.Attr And faDirectory) <> 0
        Then
          Begin
            // folder was found - proces it in depth.
            // if you don't need hierarhial search - disable this brunch
            If (SR.Name <> '.') and (SR.Name <> '..')
              Then ProcessFolder(FolderName+SR.Name+'\',SearchStr,ReplaceStr);
          End
        Else
          Begin
            // file was found
            ProcessFile(FolderName+SR.Name,SearchStr,ReplaceStr);
          End;
      iFound := FindNext(SR);
    End;
  FindClose(SR);
end;

Avatar of Zip58

ASKER

Hi lmikle

Thanks for your replay but can you not make a little demo?

Regards

Zip58
Sorry, Demo of what?

Demo search and replace in single file?
Avatar of Zip58

ASKER

Hi lmikle

What I will and if you can a little example programma from

search and replace in multi files and multi directories

Regards

Zip58
Avatar of Zip58

ASKER

Hi lmikle

What I will and if you can make a  little example programma from

search and replace in multi files and multi directories

Regards

Zip58
ASKER CERTIFIED SOLUTION
Avatar of lmikle
lmikle

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 Zip58

ASKER

Hi lmikle

Thanks for your replay my email adress is zip_58@hotmail.com

and my version is Delphi 5

Thanks in advance

Regards

Zip58

Dear Zip58.

I was sent a example to your email.
Remember - this is only example and the ProcessFile procedure is very simple and can do not work with binary or large files. I was test one on simple text files not larger than 1 Kb.

Avatar of Zip58

ASKER

Hi lmikle

Thanks for the example

if I need more info I will leave a message four you here in the forum

Thanks again

Regards

Zip58