There's some way to determine with precision if a given path is a FILE or FOLDER?
If yes, could show a example please? Thanks in advance.
There's some way to determine with precision if a given path is a FILE or FOLDER?
If yes, could show a example please? Thanks in advance.
You can use the RTL's TPath.GetAttributes()
, TFile.GetAttributes()
or TDirectory.GetAttributes()
method, eg:
uses
..., System.IOUtils;
try
if TFileAttribute.faDirectory in TPath{|TFile|TDirectory}.GetAttributes(path) then
begin
// path is a folder ...
end else
begin
// path is a file ...
end;
except
// error ...
end;
Or, you can use the Win32 API GetFileAttributes()
or GetFileAttributesEx()
function directly, eg:
uses
..., Winapi.Windows;
var
attrs: DWORD;
begin
attrs := Windows.GetFileAttributes(PChar(path));
if attrs = INVALID_FILE_ATTRIBUTES then
begin
// error ...
end
else if (attrs and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
begin
// path is a folder ...
end else
begin
// path is a file ...
end;
end;
uses
..., Winapi.Windows;
var
data: WIN32_FILE_ATTRIBUTE_DATA;
begin
if not Windows.GetFileAttributesEx(PChar(path), GetFileExInfoStandard, @data) then
begin
// error ...
end
else if (data.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
begin
// path is a folder ...
end else
begin
// path is a file ...
end;
end;
// Note: the folder must exist.
function IsFolder(CONST FullPath: string): boolean;
begin
Result:= {NOT FileExists(FullPath) AND} DirectoryExists(FullPath);
end;