// FindFiles - Klasse zum Durchsuchen von Ordnern
// Michael Puff [[url]http://www.michael-puff.de][/url]
unit FindFilesCls;
interface
uses
Windows;
type
TOnFindFile =
procedure(Filename:
string)
of object;
TOnDirectoryFind =
procedure(Directory:
string)
of object;
TFindFiles =
class(TObject)
private
FSubfolders: Boolean;
FMask:
string;
FOnFindFile: TOnFindFile;
FOnFindDirectory: TOnDirectoryFind;
public
constructor Create;
procedure Find(RootFolder:
string);
property SubFolders: Boolean
read FSubFolders
write FSubFolders;
property Mask:
string read FMask
write FMask;
property OnFileFind: TOnFindFile
read FOnFindFile
write FOnFindFile;
property OnDirectoryFind: TOnDirectoryFind
read FOnFindDirectory
write FOnFindDirectory;
end;
implementation
{ TFindFiles }
constructor TFindFiles.Create;
begin
inherited;
FSubfolders := False;
FMask := '
*.*';
end;
procedure TFindFiles.Find(RootFolder:
string);
var
wfd: TWin32FindData;
hFile: THandle;
begin
if RootFolder[Length(RootFolder)] <> '
\'
then
RootFolder := RootFolder + '
\';
if Self.SubFolders
then
begin
hFile := FindFirstFile(PChar(RootFolder + '
*.*'), wfd);
if hFile <> INVALID_HANDLE_VALUE
then
try
repeat
if wfd.dwFileAttributes
and FILE_ATTRIBUTE_DIRECTORY =
FILE_ATTRIBUTE_DIRECTORY
then
if (
string(wfd.cFileName) <> '
.')
and (
string(wfd.cFileName) <> '
..')
then
begin
if Assigned(OnDirectoryFind)
then
OnDirectoryFind(RootFolder + wfd.cFileName);
Find(RootFolder + wfd.cFileName);
end;
until FindNextFile(hFile, wfd) = False;
finally
windows.FindClose(hFile);
end;
end;
hFile := FindFirstFile(PChar(RootFolder + Self.Mask), wfd);
if hFile <> INVALID_HANDLE_VALUE
then
try
repeat
if wfd.dwFileAttributes
and FILE_ATTRIBUTE_DIRECTORY <>
FILE_ATTRIBUTE_DIRECTORY
then
begin
if Assigned(OnFileFind)
then
OnFileFind(RootFolder + wfd.cFileName);
end;
until FindNextFile(hFile, wfd) = False;
finally
Windows.FindClose(hFile);
end;
end;
end.