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?
 
thiagoblimeiraConnect With a Mentor Commented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.