unit dir;
interface
uses windows;
type
LongRec =
packed record
case Integer
of
0: (Lo, Hi: Word);
1: (Words:
array [0..1]
of Word);
2: (Bytes:
array [0..3]
of Byte);
end;
type TFileName =
string;
type
TSearchRec =
record
Time: Integer;
Size: Integer;
Attr: Integer;
Name: TFileName;
ExcludeAttr: Integer;
FindHandle: THandle;
FindData: TWin32FindData;
end;
const
faReadOnly = $00000001
platform;
faHidden = $00000002
platform;
// hier fehler
faSysFile = $00000004
platform;
faVolumeID = $00000008
platform;
faDirectory = $00000010;
faArchive = $00000020
platform;
faSymLink = $00000040
platform;
faAnyFile = $0000003F;
function FindFirst(
const Path:
string; Attr: Integer;
forward;
function FindNext(
var F: TSearchRec): Integer;
forward;
procedure FindClose(
var F: TSearchRec);
forward;
function FindMatchingFile(
var F: TSearchRec): Integer;
forward;
implementation
function FindFirst(
const Path:
string; Attr: Integer;
var F: TSearchRec): Integer;
const
faSpecial = faHidden
or faSysFile
or faVolumeID
or faDirectory;
begin
F.ExcludeAttr :=
not Attr
and faSpecial;
F.FindHandle := FindFirstFile(PChar(Path), F.FindData);
if F.FindHandle <> INVALID_HANDLE_VALUE
then
begin
Result := FindMatchingFile(F);
if Result <> 0
then FindClose(F);
end else
Result := GetLastError;
end;
function FindNext(
var F: TSearchRec): Integer;
begin
if FindNextFile(F.FindHandle, F.FindData)
then
Result := FindMatchingFile(F)
else
Result := GetLastError;
end;
procedure FindClose(
var F: TSearchRec);
begin
{$IFDEF MSWINDOWS}
if F.FindHandle <> INVALID_HANDLE_VALUE
then
begin
Windows.FindClose(F.FindHandle);
F.FindHandle := INVALID_HANDLE_VALUE;
end;
{$ENDIF}
{$IFDEF LINUX}
if F.FindHandle <>
nil then
begin
closedir(F.FindHandle);
F.FindHandle :=
nil;
end;
{$ENDIF}
end;
function FindMatchingFile(
var F: TSearchRec): Integer;
var
LocalFileTime: TFileTime;
begin
with F
do
begin
while FindData.dwFileAttributes
and ExcludeAttr <> 0
do
if not FindNextFile(FindHandle, FindData)
then
begin
Result := GetLastError;
Exit;
end;
FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
FileTimeToDosDateTime(LocalFileTime, LongRec(Time).Hi,
LongRec(Time).Lo);
Size := FindData.nFileSizeLow;
Attr := FindData.dwFileAttributes;
Name := FindData.cFileName;
end;
Result := 0;
end;
end.