Solved

how do i do this?

Posted on 2004-04-20
3
268 Views
Last Modified: 2010-04-05
I have a procedure that checks a directory for files with a certain extension, it reads the file and saves contents to another file, how do i efficiently arrange this to check multiple directorys for multiple extensions?

procedure TForm1.Button1Click(Sender: TObject);
var
  List1: TStringList;
  List2 : TStringList;
begin
  List1:= TStringList.Create;
  List2:= TStringList.Create;
  GetFileList(List1, 'C:\', 'TXT*');
  List2.CommaText := ProcessFiles(List1);
  List2.SaveToFile('c:\myfile.log');
  List1.Free;
end;
end.
0
Comment
Question by:KLOPEKS
3 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 10866611
What you want is very close to:
http://experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20932619.html

Download from:        http://www.geocities.com/esoftbg/ee/Q_20932619.zip working example of above question
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 10866628
an older sample
which can use multiple filemasks

unit filesearch_with_masks_u;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    ListBox1: TListBox;
    Button1: TButton;
    CheckBox1: TCheckBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses filectrl; //just for selectdirectory

procedure FindFile(APath : string;
                  IncludeSubDirs : Boolean;
                  FileMasks : TStrings;
                  ResultList : Tstrings
                  );
var
  srec : TSearchRec;
  Path, FileName : String;
  i : integer;
begin
  path := APath;
  if path[length(path)] <> '\' then
    path := path + '\';

  if not assigned(FileMasks) then
    raise exception.Create('No Masks given');
  if not assigned(ResultList) then
    raise exception.Create('No ResultContainer given');
  for i := 0 to filemasks.Count - 1 do
  begin
  // CurrentDirectory
    if findfirst(Path+filemasks[i],faanyfile,srec) = 0 then
    begin
    repeat
      If Not(srec.attr and fadirectory > 0) then
      begin
        ResultList.Add(Path+SRec.Name)
      end;
    Until findnext(srec) <> 0;
    end;
    FindClose(SRec);
    { SubDirectories }
    If IncludeSubDirs then
    begin
    if FindFirst(Path+'*.*',FaAnyFile,Srec) = 0 then
    begin
      repeat
        // StartRecursion
        if not(Srec.Name[1] = '.') and (srec.attr and fadirectory > 0)then
          FindFile(Path + Srec.Name + '\' + FileName,IncludeSubDirs,FileMasks,ResultList)
      Until findnext(srec) <> 0;
    end;
    FindClose(SRec);
    end;
  end;
end;


//usage sample
procedure TForm1.Button1Click(Sender: TObject);
var path : String;
begin
  if selectDirectory('Select Directory','',path) then
  begin
    listbox1.Items.Clear;
    findfile(path,checkbox1.Checked,memo1.lines,listbox1.items);
    showmessage(inttostr(listbox1.Items.Count)+' files found');
  end;
end;

end.

meikl ;-)
0
 
LVL 3

Accepted Solution

by:
MikProg earned 75 total points
ID: 10866672

   procedure SeekAndDestroy(PathList, Extensions: TStrings) :)
   var
     Subpaths: TStringList;
          procedure GetFileList(Path);
          var
             List1: TStringList;
             List2 : TStringList;
         begin
           List1:= TStringList.Create;
          {fill list1 with filenames using Path and  Extension variable}
           List2:= TStringList.Create;
           List2.CommaText := ProcessFiles(List1);
           List2.SaveToFile('c:\myfile.log');
           List1.Free;
           List2.Free;
          end;
  begin
   { For Each PathList string }
      GetFileList(PathList[i])  
      Subpaths:=TStringList.Create;
      {Fill Subpaths with subdirs names of PathList[i] directory}
      SeekAndDestroy(Subpaths,Extensions);
  end;

procedure TForm1.Button1Click(Sender: TObject);
  var
    List1: TStringList;
    List2 : TStringList;
begin
{Fill List1 with path's to search. Noone of them can't be sub of another}
{Fill List2 with extensions}
  SeekAndDestroy(List1, List2);
end.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now