Delphi-Quellcode:
function IsDir(Dir: string): Boolean;
var
wfd: TWin32FindData;
hFile: THandle;
begin
hFile := FindFirstFile(PChar(Dir), wfd);
result := ((wfd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) =
FILE_ATTRIBUTE_DIRECTORY) and (hFile = INVALID_HANDLE_VALUE);
if hFile <> INVALID_HANDLE_VALUE then
Windows.FindClose(hFile);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsDir('E:\Delphi\Programme') then
ShowMessage('Verzeichnis')
else
ShowMessage('Datei');
end;
Wird eine Datei angegeben, die nicht existiert, liefert die Funktion auch
wahr zurück.
[Edit]
Mist, da ist DirectoryExists besser.
[Edit2]
Verbessert.
[Edit3]
Habe noch was besseres von Assarbad gefunden:
Delphi-Quellcode:
function FileExists(
const FileName:
string; dir: boolean = false): Boolean;
var
hidate, lodate: word;
Handle: THandle;
FindData: TWin32FindData;
LocalFileTime: TFileTime;
type
LongRec =
packed record
Lo, Hi: Word;
end;
function SubFileExists: Boolean;
begin
FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
result := FileTimeToDosDateTime(LocalFileTime, HiDate, LoDate);
end;
begin
result := false;
Handle := FindFirstFile(PChar(FileName), FindData);
if Handle <> INVALID_HANDLE_VALUE
then
begin
Windows.FindClose(
Handle);
case dir
of
TRUE:
if (FindData.dwFileAttributes
and FILE_ATTRIBUTE_DIRECTORY) <> 0
then result := SubFileExists;
FALSE:
if (FindData.dwFileAttributes
and FILE_ATTRIBUTE_DIRECTORY) = 0
then result := SubFileExists;
end;
end;
end;