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;
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;
var
list: TStringList;
begin
list := FindFile('C:\Delphi\*.pas');
ListBox1.Items.Assign(list);
list.Free;
end;
Another posts included in "Delphi"
| How to turn off monitor ? (0) | 2008/12/10 |
| How to get screen resolution in case of using multiple monitors ? (0) | 2008/12/16 |
| Save TBitmap image to Jpeg format image in Delphi (0) | 2009/06/26 |
| Sending email messages in .Net (0) | 2007/10/04 |
| Implementing C#'s foreach loop in Delphi 8 (0) | 2007/10/04 |
| Delphi for .Net Code Folding keyboard shortcuts (0) | 2007/10/04 |
| How to encrypt a string (0) | 2007/10/04 |
| Creating thumbnail images (0) | 2007/10/04 |

Prev

Rss Feed