Getting file extensions in a folder

I have a folder containing files of various types.  If any of these types are either .cgi or .pl files, I want a boolean variable called "FileType" to be set to true.

Also any .cgi or .pl files in any subfolders should also set this to true.

I am using Delphi 6.  Any help would be greatly appreciated.
rincewind666Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

thiagoblimeiraCommented:
try this function:

function FileSearch(const PathName, FileName : string; const InDir : boolean): Boolean;
var Rec  : TSearchRec;
    Path : string;
begin
  Result := False;
  Path := IncludeTrailingBackslash(PathName);
  if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
    try
      repeat
      Result:= True;
      until FindNext(Rec) <> 0;
    finally
      FindClose(Rec);
    end;

  if not InDir then Exit;

  if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
   try
     repeat
      if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
       FileSearch(Path + Rec.Name, FileName, True);
     until FindNext(Rec) <> 0;
   finally
     FindClose(Rec);
   end;
end;

if FileSearch(S, '*.cgi', False) then ShowMessage('Has cgi');
if FileSearch(S, '*.pl', False) then ShowMessage('Has pl');
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thiagoblimeiraCommented:
sorry S is the path to your folder
if FileSearch(S, '*.cgi', False) then ShowMessage('Has cgi');
if FileSearch(S, '*.pl', False) then ShowMessage('Has pl');
0
systanCommented:
//Unit1.pas
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Memo1: TMemo;
    ListBox2: TListBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure TFindFile(APath : string; FileMasks : TStrings; ResultList : Tstrings );
    function showdrives: string;



  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  isscan:boolean;

implementation

{$R *.dfm}


function TForm1.showdrives: string;
var
  Drive: Char;
  a:string;
begin
Listbox1.clear;
  For Drive := 'A' To 'Z' Do
  Begin
   if (GetDriveType(PChar(Drive + ':\'))= DRIVE_FIXED) OR (GetDriveType(PChar(Drive + ':\'))= DRIVE_REMOVABLE) then
   begin
        Listbox1.Items.Add(Format('%s:\',[Drive]) );
   end;
   end;
a := Listbox1.Items[Listbox1.ItemIndex+1];
result:=a;
end;


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

  for i := 0 to (filemasks.Count - 1) do
  begin
    if findfirst(Path+filemasks[i],faAnyFile - faDirectory,srec) = 0 then
    begin
   repeat
      if pos('.pl',SRec.Name)>0 then ResultList.Add('PERL Extension is: ' + Path+SRec.Name)
      else if pos('.cgi',SRec.Name)>0 then ResultList.Add('CGI Extension is: ' + Path+SRec.Name)
      else
      ResultList.Add(Path+SRec.Name);
      application.ProcessMessages;
    if isscan = false then exit;
    Until findnext(srec) <> 0;
  end;
FindClose(SRec);
end;
    if FindFirst(Path+'*.*',FaAnyFile,Srec) = 0 then
    begin
      repeat
        if ((sRec.Attr and faDirectory) <> 0)  and (sRec.Name<>'.') and (sRec.Name<>'..') then
          TFindFile(Path + Srec.Name + '\' + FileName, FileMasks,ResultList);
    Until findnext(srec) <> 0;
    end;
    FindClose(SRec);
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Tfindfile(showdrives, Listbox2.items, memo1.lines);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
isscan:=false;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Listbox2.Items.Add('*.*');
Showdrives;
isscan:=True;
end;

end.



//program1.dpr
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
0
rincewind666Author Commented:
Many thanks for your help, which is greatly appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.