2008/10/10 13:08

Find File at certain directory in Delphi


This function we show below receives as parameters a file specification (like for example 'C:\tmp\*.tmp' or 'C:\*' if you want to search the entire hard disk.) and optionally a set of attributes (exactly as Delphi's FindFirst function), and it returs a StringList with the full pathnames of the found files. You should free the StringList after using it.

interface

uses SysUtils, Classes;

function FindFile(const filespec: TFileName; attributes: integer
   = faReadOnly Or faHidden Or faSysFile Or faArchive): TStringList;

implementation

function FindFile(const filespec: TFileName; attributes: integer): TStringList;
var
  spec: string;
  list: TStringList;

procedure RFindFile(const folder: TFileName);
var
  SearchRec: TSearchRec;
begin
  // Locate all matching files in the current
  // folder and add their names to the list
  if FindFirst(folder + spec, attributes, SearchRec) = 0 then begin
    try
      repeat
        if (SearchRec.Attr and faDirectory = 0) or
           (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
          list.Add(folder + SearchRec.Name);
      until FindNext(SearchRec) <> 0;
    except
      FindClose(SearchRec);
      raise;
    end;
    FindClose(SearchRec);
  end;
  // Now search the subfolders
  if FindFirst(folder + '*', attributes
      Or faDirectory, SearchRec) = 0 then
  begin
    try
      repeat
        if ((SearchRec.Attr and faDirectory) <> 0) and
           (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
          RFindFile(folder + SearchRec.Name + '\');
      until FindNext(SearchRec) <> 0;
    except
      FindClose(SearchRec);
      raise;
    end;
    FindClose(SearchRec);
  end;
end; // procedure RFindFile inside of FindFile

begin // function FindFile
  list := TStringList.Create;
  try
    spec := ExtractFileName(filespec);
    RFindFile(ExtractFilePath(filespec));
    Result := list;
  except
    list.Free;
    raise;
  end;
end;


Sample call

You can try this function placing a ListBox and a button on a form and adding this code to the OnClick event of the button:

procedure TForm1.Button1Click(Sender: TObject);
var
  list: TStringList;
begin
  list := FindFile('C:\Delphi\*.pas');
  ListBox1.Items.Assign(list);
  list.Free;
end;
Trackback 6 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Online phentermine.

    Tracked from Cheap phentermine. 2010/07/15 17:38 delete

    Phentermine cod.

  2. Subject Cialis.

    Tracked from Cialis american pharmacy. 2010/07/27 15:56 delete

    Cialis cocaine combination. Buy cialis. Cialis.

  3. Subject Ambien cr.

    Tracked from Buy ambien. 2010/07/27 17:09 delete

    Buy ambien 12.5 mgm overnight mail md consultation. Ambien message board. What is ambien. Ambien overnight. Ambien buy. Ambien.

  4. Subject Taking viagra with cialis.

    Tracked from Cialis. 2010/07/27 21:21 delete

    Dangers cialis and cocaine. Cialis.

  5. Subject Ambien addiction.

    Tracked from Ambien. 2010/07/28 13:24 delete

    Ambien side effects. Ambien withdrawal. Ambien cr.

  6. Subject Britney spears without underwear.

    Tracked from Men's underwear. 2010/08/04 10:27 delete

    Mens sexy underwear. Calvin klein underwear. Men's underwear.