• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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
0
Zip58
Asked:
Zip58
  • 5
  • 4
1 Solution
 
lmikleCommented:
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;

0
 
Zip58Author Commented:
Hi lmikle

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

Regards

Zip58
0
 
lmikleCommented:
Sorry, Demo of what?

Demo search and replace in single file?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Zip58Author Commented:
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
0
 
Zip58Author Commented:
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
0
 
lmikleCommented:
Hi Zip58.

The code above implement a basic alorithm to perform search and replace in a list of folders with subfolders.

In a visual part of program you simple must fill a source folders list and search and replace string. After than on any buttton click you mast call ProcessFolders procedure and pass into it a folders list and search and replace strings. Please post here your email address. I'll send you a little example (also please post a your Delphi version - curently i have installed versions 5 and 7 and can make this example on any of this version)
0
 
Zip58Author Commented:
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

0
 
lmikleCommented:
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.

0
 
Zip58Author Commented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now